1
我有這個集合插入收集到列表與嵌套數組
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" } ]
}
]
})
我試着將它插入到在C#中的清單,但我有一個嵌套數組watchtbl和symboles。我不知道它是如何工作來獲取元素到列表或顯示它 我試試這個代碼
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;
});
}
}
}
}
以及類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; }
}
我的問題是如何從文件到列表中得到Watchtbl,Symboles太 但我們的代碼進行了優化,並幫助我在獲取數據的快速,日Thnx:d – Juste3alfaz
我嘗試這樣的事情,BsonValue wtstbl =批[」 Watchtbl「];列表 wtstblcls = BsonSerializer.Deserialize <列表>(wtstbl.ToJson());然後我添加到watchtbl,WatchTbls = BsonSerializer.Deserialize <列表>(wtstbl.ToJson()) –
Juste3alfaz
@ Juste3alfaz我已更新帖子 – ntohl