【发布时间】:2017-10-06 22:47:08
【问题描述】:
我有一个结构如下的数据模型:
{
"_id": "1234abcd",
"name": "The Stanley Parable"
"notes": [
{
"_id": "5678efgh",
"content": "Kind of a walking simulator."
},
{
"_id": "5678efgh",
"content": "Super trippy."
},
]
}
我正在使用 C# 的 mongo 驱动程序与 .NET Core 中的此模型进行交互。我正在尝试为特定游戏添加新注释 - 不一定确定该游戏已经有任何注释。这是我能够找到的添加到嵌套列表的内容:
var filter = Builders<Mongo_VideoGame>.Filter.Eq("Id", id);
var update = Builders<Mongo_VideoGame>.Update.Push<Mongo_Note>(v => v.Notes, mongoNote);
var editedGame = _context.VideoGames.FindOneAndUpdate(filter, update);
但我的问题是,只有在游戏模型上已有“笔记”元素时才有效。我可以在制作原始游戏时添加一个空数组,但更改架构似乎不太灵活(即,如果我向现有数据添加注释怎么办)。
An exception of type 'MongoDB.Driver.MongoCommandException' occurred in MongoDB.Driver.Core.dll but was not handled in user code: 'Command findAndModify failed: The field 'notes' must be an array but is of type null in document'
【问题讨论】: