1

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; }
    }

1 Answer 1

1

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

My problem how to get Watchtbl from the document into list, Symboles too But ur code is optimized and help me in get data fast, thnx :D
I try something like this, BsonValue wtstbl = batch["Watchtbl"]; List<WatchTblCls> wtstblcls = BsonSerializer.Deserialize<List<WatchTblCls>>(wtstbl.ToJson()); and thena I add to watchtbl, WatchTbls = BsonSerializer.Deserialize<List<WatchTblCls>>(wtstbl.ToJson())
You are my savor big thnx :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.