开发者

Insert document inside another document

how can I insert a document inside another document with mongodb ?

I've tried something like the code below but I always have a problem converting my list of setting in the field Settings:

var settingViewModels = new[]{ setting };
var query = Query.EQ("Name", applicationName);
var update = Update.AddToSet("Settings", BsonArray.Create(setting));
db.Collection<Applications>().Update(query, update, UpdateFlags.Upsert, SafeMode.True)

I got an error when I convert my settingViewModels into BsonArray saying:

.NET type开发者_开发技巧 MyProject.SettingViewModel cannot be mapped into BsonType.Array Parameter name: value

Any idea ?

thanks John


If you want add setting item to the Settings[] array of Application collection you should use ToBsonDocument() extention method from MongoDB.Bson namespace:

var update = Update.AddToSet("Settings", setting.ToBsonDocument());

You no need create BsonArray.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜