【发布时间】:2021-05-13 09:49:23
【问题描述】:
我无法理解如何在 mongoDB 的数组对象中更新标签数组。
我有一个对象数组及其对应的 ObjectID,我想在 mongoDB 上更新哪些标签。我尝试的是使用一个循环,使用 updateOne 指定 ObjectID 作为查询来查找相应的文档。
类似这样的:
nodes.forEach(node => {
const query = { '_id' : ObjectId(node.Id) }
db.collection(collection).updateOne(
query,
{ $set: { 'tags': value } },
function(err, result) {
if (err) {
throw err;
}
console.log(result);
});
});
不幸的是,这不起作用。我尝试了其他方法,但找不到任何有效的方法
我有以下mongodb数据结构:
{
"_id": {
"$oid": "6022bc9f9b55276bd39f4e0"
},
"name": "TestProject",
"projectData": {
"nodes": [
{
"data": {
"_id": {
"$oid": "6022bc959b559276bd39f4be"
},
"nodeData": {
"tags": []
}
}
},
{
"data": {
"_id": {
"$oid": "7022bc959b559276bd39f4ce"
},
"nodeData": {
"tags": []
}
}
},
{
"data": {
"_id": {
"$oid": "8022bc959b559276bd39f4de"
},
"nodeData": {
"tags": []
}
}
}
....
]
}
我只想更新与我作为 ObjectID 传入的对象匹配的特定对象标签。 更新与特定 ObjectID 匹配的一堆对象值的最佳方法是什么?
【问题讨论】: