[CSHARP-2121] Unable to retrieve nested child array Created: 07/Dec/17  Updated: 05/Apr/19  Resolved: 25/Feb/19

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: justin teaw Assignee: Wan Bachtiar
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

My json document contains nested array.

{
    "_id" : ObjectId("5a26b43b38a9447d8032e8a2"),
    "personId" : UUID("50aa744d-95c3-4b77-9ffe-b2593c6d9732"),
    "address" : {
        "state" : "california"
    },
	"contacts" : [
		{ "name" : "John" },
		{ "name" : "Bob" }
	]
}

I get an this error.

 
{"Value type of serializer is System.Collections.Generic.List`1[[Model.V1.Contacts, PlatformAPI.Monitoring.Core, Version=1.2.0.403, Culture=neutral, PublicKeyToken=null]] and does not match member type System.Collections.Generic.IEnumerable`1[[Model.V1.Contacts, PlatformAPI.Monitoring.Core, Version=1.2.0.403, Culture=neutral, PublicKeyToken=null]].\r\nParameter name: serializer"}



 Comments   
Comment by Wan Bachtiar [ 29/Jan/19 ]

Hi Justin,

Could you elaborate the issue of retrieving nested child array by providing:

Given the sample document that you have, you could utilise below example to retrieve the nested child array document:

        public class Person 
        {
            public ObjectId Id {get;set;}
            public System.Guid personId {get; set;}
            public Address address {get; set;}
            public List<Contact> contacts {get; set;}
        }
        
        public class Address
        {
            public string state {get; set;}
        }
 
        public class Contact
        {
            public string name {get; set;}
        }
 
        static void Main(string[] args)
        {
            var mongoURL = new MongoUrl("<MONGODB URI>");
            var client = new MongoClient(mongoURL);
            var database = client.GetDatabase("databaseName");       
            var collection = database.GetCollection<Person>("collectionName");
 
            var filter = Builders<Person>.Filter.Empty;
            var document = collection.Find<Person>(filter).FirstOrDefault(); 
 
            // Fetch first array element in contacts 
            Console.WriteLine(document.contacts[0].ToJson()); 
        }

Please note that the CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have any follow-up questions on the use of the driver, please post a question on mongodb-user group with relevant the information.

Regards,
Wan.

Generated at Wed Feb 07 21:41:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.