我有收集,结构如下, 如何使用
更新tags
字段
_id: 556c1c836e80082821f41d9f
_versions.id: 556c1c836e80082821f41da6
_versions._data._id: 556c71735ddb5010067f1beb
[{
"_id": ObjectId("556c1c836e80082821f41d9f");
"_name": "Product 1"
"_curr_version": ObjectId("556c1c836e80082821f41da6")
"_versions": [{
"_id": ObjectId("556c71735ddb5010067f1be6"),
"_data": [{
"_id": ObjectId("556c1cae82b9a3101787d20f"),
"_tags": ['sport', 'football']
}, {
"_id": ObjectId("556c71735ddb5010067f1beb"),
"tags": ['golf']
}
]
},
{
"_id": ObjectId("556c1c836e80082821f41da6"),
"_data": [{
"_id": ObjectId("556c1cae82b9a3101787d20f"),
"_tags": ['sport', 'swimming']
}, {
"_id": ObjectId("556c71735ddb5010067f1beb"),
"tags": ['tenis']
}
]
}
]
}]
答案 0 :(得分:0)
我在nodejs上工作,并使用mongosee驱动程序访问mongodb。我找到了解决方案。
_versions._data._id: 556c71735ddb5010067f1beb
位置存储为pos
var 之后我有:
var ObjectId = require('mongoskin').ObjectID;
var setObject = {};
setObject["_versions.$._data."+ pos +"._tag"] = ['a','b'];
db.use("MongoDB").collection('collections').update({
"_id": new ObjectId('556c1c836e80082821f41d9f'),
"_versions._id": new ObjectId('556c1c836e80082821f41da6')
},
{
$set : setObject
},
{},
cb);