How to insert nested document using .NET SDK

40-beta

#1

I have a document like that:

{                    
id :1,
type : "post",
title =: "Firt Post",
content : "This is first post to this blog",
author : "Sonnt",
comments : [
{
 id : 1,
 comment : "This is a comment 1"
},
{
 id : 2,
 comment : "This is a comment 2"
}
]
}

And i use .NET client 2.1 to insert to couchbase server, following : http://docs.couchbase.com/developer/dotnet-2.1/hello-couchbase.html

var result = bucket.Insert(new Document<dynamic>
            {
                Id = "test_doc_01",
                Content = new
                {
                    id = 1,
                    type = "post",
                    title = "Firt Post",
                    content = "This is first post to this blog",
                    author = "sonnt",
                    comments = [
                        {
                         id = 1,
                         comment = "this a comment 1"   
                        },
                        {
                    id = 2,
                    comment = "this a comment 2"
                }
                    ]
                }
            });

But i get error. What is right format for this document.


#2

Hi @Sonrobby,
I think you get a null reference exception (correct me if I’m wrong) but this is likely due to the missing “new” keyword in front of the “[” for the “comments” array.


#3

@martinesmann This isn’t correct syntax. I change some syntax, but no any one correct.
Can’t any body help me transform document to C#.NET syntax…


#4

Hi @Sonrobby,
Sure :slight_smile: here is a correct formated document:

var doc = new Document
{
Id = “test_doc_01”,
Content = new
{
id = 1,
type = “post”,
title = “Firt Post”,
content = “This is first post to this blog”,
author = “sonnt”,
comments =
new []{
new {
id = 1,
comment = “this a comment 1”
},
new {
id = 2,
comment = “this a comment 2”
}
}

            }
        };

#5

You can also find more documentation and a bit more explanation on implicit types and array’s at MSDN, here:
https://msdn.microsoft.com/en-us/library/bb384090.aspx


#6

Thank @martinesmann. It’s work for me.