简体   繁体   中英

Insert collection into list with nested array

I have this collection

    db.UserWatchtbl.insert( { 
fbId: "", 
Name: "user1", 
pass: "pass1",
Watchtbl: 
    [ 
        { 
        wid: "1350",
        name: "bought stock1",
        Symboles: [ { Name: "AAA" }, { Name: "BSI" } ] 
        },
        { 
        wid: "1350",
        name: "bought stock2",
        Symboles: [ { Name: "AAA" }, { Name: "BSI" }, { Name: "EXXI" } ] 
        }
    ]
} )

I try to insert it into list in C#, but I have a nested array watchtbl and symboles. I don't know how it works to get that element into list or to show it I try this code

private async void Master_BindData()
    {
        // add user into datagridview from MongoDB Colelction Watchtbl
        var client = new MongoClient("mongodb://servername:27017");

        var database = client.GetDatabase("WatchTblDB");
        var collectionWatchtbl = database.GetCollection<BsonDocument>("UserWatchtbl");

        var filter = new BsonDocument();
        using (var cursor = await collectionWatchtbl.FindAsync(filter))
        {
            while (await cursor.MoveNextAsync())
            {
                var batch = cursor.Current;
                foreach (var document in batch)
                {

                    user.Add(new UserWatchTblCls()
                    {
                        Id = ObjectId.Parse(document["_id"].ToString()),
                        Name = document["Name"].ToString()
                        //WatchTbls = document["Watchtbl"].AsBsonArray;
                    });
                }
            }
        }
    }

and for the class UserWatchTblCls()

public class UserWatchTblCls
    {
        [BsonId]
        public ObjectId Id { get; set; }
        public string fbId { get; set; }
        public string Name { get; set; }
        public string Pass { get; set; }
        public List<WatchTblCls> WatchTbls { get; set; }
    }

    public class WatchTblCls
    {
        public string WID { get; set; }
        public string Name { get; set; }
        public List<SymboleCls> Symbols { get; set; }
    }

    public class SymboleCls
    {
        public string Name { get; set; }
    }

You have to iterate through the Result of FindAsync . This will collect the data into user ( List<UserWatchTblCls> )

var user = new List<UserWatchTblCls>();

var cursor = collectionWatchtbl.FindAsync(filter).Result;
cursor.ForEachAsync(batch =>
{
    user.Add(new UserWatchTblCls()
    {
        Id = ObjectId.Parse(batch["_id"].ToString()),
        Name = batch["Name"].ToString()
    });
});

EDIT

Ohh. So You are looking for deep deserialization too.

public ReadFromDB()
{
    var client = new MongoClient("mongodb://localhost:27017");

    var database = client.GetDatabase("test");
    var collectionWatchtbl = database.GetCollection<BsonDocument>("UserWatchtbl");

    var filter = new BsonDocument();
    var user = new List<UserWatchTblCls>();
    var cursor = collectionWatchtbl.FindAsync(filter).Result;
    cursor.ForEachAsync(batch =>
    {
        user.Add(BsonSerializer.Deserialize<UserWatchTblCls>(batch));
    });
}

public class UserWatchTblCls
{
    [BsonId]
    [BsonElement("_id")]
    public ObjectId Id { get; set; }
    public string fbId { get; set; }
    public string Name { get; set; }
    [BsonElement("pass")]
    public string Pass { get; set; }
    [BsonElement("Watchtbl")]
    public List<WatchTblCls> WatchTbls { get; set; }
}

public class WatchTblCls
{
    [BsonElement("wid")]
    public string WID { get; set; }
    [BsonElement("name")]
    public string Name { get; set; }
    [BsonElement("Symboles")]
    public List<SymboleCls> Symbols { get; set; }
}

public class SymboleCls
{
    public string Name { get; set; }
}

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