San*_*vel 3 arrays api mongodb node.js
我有以下mongodb文档结构:
[
{
"_id": "04",
"name": "test service 4",
"id": "04",
"version": "0.0.1",
"title": "testing",
"description": "test",
"protocol": "test",
"operations": [
{
"_id": "99",
"oName": "test op 52222222222",
"sid": "04",
"name": "test op 52222222222",
"oid": "99",
"description": "testing",
"returntype": "test",
"parameters": [
{
"oName": "Param1",
"name": "Param1",
"pid": "011",
"type": "582",
"description": "testing",
"value": ""
},
{
"oName": "Param2",
"name": "Param2",
"pid": "012",
"type": "58222",
"description": "testing",
"value": ""
}
]
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我已经能够使用$ elemMatch来更新操作中的字段,但是当我尝试对参数执行相同的操作(修改)时,它似乎不起作用.我想知道我应该尝试哪种其他方法,以便能够成功更新特定参数中的字段,通过其pid查找它.
我目前拥有且不起作用的更新代码如下所示:
var oid = req.params.operations;
var pid = req.params.parameters;
collection.update({"parameters":{"$elemMatch": {"pid": pid}}},{"$set": {"parameters.$.name":req.body.name, "parameters.$.description": req.body.description,"parameters.$.oName": req.body.oName,"parameters.$.type": req.body.type} }, function(err, result) {
if (err) {
console.log('Error updating service: ' + err);
res.send({'error':'An error has occurred'});
} else {
// console.log('' + result + ' document(s) updated');
res.send(result);
}
});
Run Code Online (Sandbox Code Playgroud)
chr*_*dam 14
使用MongoDB 3.6及更高版本时,新功能允许您使用位置过滤$\[<identifier>\]
语法更新嵌套数组,以匹配特定元素并arrayFilters
在update语句中应用不同的条件:
const { oid, pid } = req.params;
const { name, oName, description, type } = req.body;
collection.update(
{
"_id": 1,
"operations": {
"$elemMatch": {
oid, "parameters.pid": pid
}
}
},
{ "$set": {
"operations.$[outer].parameters.$[inner].name": name,
"operations.$[outer].parameters.$[inner].description": description,
"operations.$[outer].parameters.$[inner].oName": oName,
"operations.$[outer].parameters.$[inner].type": type
} },
{ "arrayFilters": [
{ "outer.oid": oid },
{ "inner.pid": pid }
] }, (err, result) => {
if (err) {
console.log('Error updating service: ' + err);
res.send({'error':'An error has occurred'});
} else {
// console.log('' + result + ' document(s) updated');
res.send(result);
}
});
Run Code Online (Sandbox Code Playgroud)
正如@wdberkeley在评论中提到的那样:
MongoDB不支持匹配到一个以上的数组级别.考虑更改文档模型,以便每个文档代表一个操作,其中包含操作文档中重复的一组操作的共同信息.
我同意上述内容并建议重新设计您的模式,因为MongoDB引擎不支持多个位置运算符(请参阅多次使用位置$
运算符来更新嵌套数组)
但是,如果您知道具有要事先更新的参数对象的操作数组的索引,则更新查询将为:
db.collection.update(
{
"_id" : "04",
"operations.parameters.pid": "011"
},
{
"$set": {
"operations.0.parameters.$.name": "foo",
"operations.0.parameters.$.description": "bar",
"operations.0.parameters.$.type": "foo"
}
}
)
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您想动态创建$set
条件,即可以帮助您获取对象的索引然后相应地进行修改,那么请考虑使用MapReduce.
目前,使用聚合框架似乎无法做到这一点.有一个未解决的开放JIRA问题与之相关联.但是,MapReduce可以解决方法.MapReduce的基本思想是它使用JavaScript作为其查询语言,但这往往比聚合框架慢得多,不应该用于实时数据分析.
在MapReduce操作中,您需要定义几个步骤,即映射步骤(将操作映射到集合中的每个文档,操作可以不执行任何操作或使用键和投影值发出一些对象)和减少步骤(获取发射值列表并将其减少为单个元素.
对于映射步骤,理想情况下,您希望获取集合中的每个文档,每个operations
数组字段的索引以及包含$set
键的另一个键.
您的reduce步骤将是一个简单定义为的函数(无效) var reduce = function() {};
然后,MapReduce操作的最后一步将创建一个单独的集合操作,其中包含已发出的操作数组对象以及包含$set
条件的字段.在原始集合上运行MapReduce操作时,可以定期更新此集合.总而言之,这个MapReduce方法看起来像:
var map = function(){
for(var i = 0; i < this.operations.length; i++){
emit(
{
"_id": this._id,
"index": i
},
{
"index": i,
"operations": this.operations[i],
"update": {
"name": "operations." + i.toString() + ".parameters.$.name",
"description": "operations." + i.toString() + ".parameters.$.description",
"type": "operations." + i.toString() + ".parameters.$.type"
}
}
);
}
};
var reduce = function(){};
db.collection.mapReduce(
map,
reduce,
{
"out": {
"replace": "operations"
}
}
);
Run Code Online (Sandbox Code Playgroud)
operations
从MapReduce操作查询输出集合通常会为您提供结果:
db.operations.findOne()
Run Code Online (Sandbox Code Playgroud)
输出:
{
"_id" : {
"_id" : "03",
"index" : 0
},
"value" : {
"index" : 0,
"operations" : {
"_id" : "96",
"oName" : "test op 52222222222",
"sid" : "04",
"name" : "test op 52222222222",
"oid" : "99",
"description" : "testing",
"returntype" : "test",
"parameters" : [
{
"oName" : "Param1",
"name" : "foo",
"pid" : "011",
"type" : "foo",
"description" : "bar",
"value" : ""
},
{
"oName" : "Param2",
"name" : "Param2",
"pid" : "012",
"type" : "58222",
"description" : "testing",
"value" : ""
}
]
},
"update" : {
"name" : "operations.0.parameters.$.name",
"description" : "operations.0.parameters.$.description",
"type" : "operations.0.parameters.$.type"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用db.operations.find()
方法中的光标迭代并相应地更新集合:
var oid = req.params.operations;
var pid = req.params.parameters;
var cur = db.operations.find({"_id._id": oid, "value.operations.parameters.pid": pid });
// Iterate through results and update using the update query object set dynamically by using the array-index syntax.
while (cur.hasNext()) {
var doc = cur.next();
var update = { "$set": {} };
// set the update query object
update["$set"][doc.value.update.name] = req.body.name;
update["$set"][doc.value.update.description] = req.body.description;
update["$set"][doc.value.update.type] = req.body.type;
db.collection.update(
{
"_id" : oid,
"operations.parameters.pid": pid
},
update
);
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11826 次 |
最近记录: |