简体   繁体   中英

How to create subdocument in MongoDB with C#

I want to create db stucture (inserting data) via C# with subdocuments. Something similar to picture attached below

Robmongo

在此处输入图片说明

I'm using this way to add "normal" kind of db

     var task = collection.InsertOneAsync(new person
           {
                Id = setid,  
                FirstName = name,
                LastName= lastname,
                Email = email,
           });

But I have no clue how to add subdocs to it.

Try something as below:-

 var task = collection.InsertOneAsync(new person
       {
            Id = setid,  
            FirstName = name,
            LastName= lastname,
            Email = email,
            playlists = [ //Array of objects
              {videos:[  //Array of objects
                {
                   title:"1", description:""
                },
                {
                   title:"2", description:""
                }
            ]}
          ]
       });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM