【发布时间】:2015-07-10 09:28:47
【问题描述】:
我有一个包含多个遵循此结构的文档的集合:
{
"_id" : {
"oid" : XXX
},
"name" : "Name",
"videos" [
{
"id" : 1,
"thumbnail" : "thumbnail.jpg",
"name" : "Name here"
},
{
"id" : 2,
"thumbnail" : "thumbnail.jpg",
"name" : "Name here"
},
{
"id" : 3,
"thumbnail" : "thumbnail.jpg",
"name" : "Name here"
}
]
}
我想查找和更新一个视频的缩略图,我只知道它的 id,但不知道它在哪个文档中。
这是我迄今为止尝试过的,但它无法正常工作。我发现的所有示例都依赖于知道文档 id 以及要更新的对象的数组位置。我还发现做这样的查询发现文档没问题,但是将整个文档设置为新的缩略图!
db.collection(COLLECTION-NAME, function(err, collection){
collection.update(
{ 'videos.id' : 2 },
{ $set: { thumbnail: "newThumbnail.jpg" } },
function(err, result){
if (!err){
console.log('saved', result)
} else {
console.log('error', err);
}
}
);
});
【问题讨论】:
标签: mongodb mongodb-query nosql