Node.js 来自多个集合的聚合中的mongodb更新查询,无需分组
我是否可以根据不同集合中的匹配键更新特定集合中的特定字段,即假设我有3个集合Node.js 来自多个集合的聚合中的mongodb更新查询,无需分组,node.js,mongodb,aggregation-framework,Node.js,Mongodb,Aggregation Framework,我是否可以根据不同集合中的匹配键更新特定集合中的特定字段,即假设我有3个集合 **///collection 1: col1///** _id:ObjectId("#####7b") name:'name1', itemsBought: [ { "_id":ObjectId("####c1" "itemName" : "name1", "itemCode" : "IT001",
**///collection 1: col1///**
_id:ObjectId("#####7b")
name:'name1',
itemsBought:
[
{
"_id":ObjectId("####c1"
"itemName" : "name1",
"itemCode" : "IT001",
"itemQuantity" : 19,
"itemPrediction":23
},
{
"_id":ObjectId("####c2"
"itemName" : "name2",
"itemCode" : "IT002",
"itemQuantity" : 79,
"itemPrediction":69
},
{
"_id":ObjectId("####c3"
"itemName" : "name2",
"itemCode" : "IT003",
"itemQuantity" : 0,
"itemPrediction":0
},
]
**///collection 1: col2///**
{
"itemQuantity" : 21,
"itemCode" : "IT001",
},
{
"itemQuantity" : 2,
"itemCode" : "IT003",
}
**///collection 1: col3///**
{
"itemCode" : "IT001",
"itemPrediction":23
},
{
"itemCode" : "IT002",
"itemPrediction":12
},
{
"itemCode" : "IT003",
"itemPrediction":7
},
我正在使用$aggregation$lookup提取所有必需的数据,在将其发送到前端之前,我需要从col2获取itemQuantity的值,从col3获取itemPrediction的值,并使用匹配的itemCode更新col1中的值。因此,我有一个从所有集合中提取所有数据的查询,但我不知道如何使用$set更新col1中的值。解决方法:您可以执行聚合并手动保存结果
db.col1.aggregate([
{
$lookup: {
from: "col2",
let: {
root_itemCode: "$itemsBought.itemCode"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$itemCode",
"$$root_itemCode"
]
}
}
}
],
as: "col2"
}
},
{
$lookup: {
from: "col3",
let: {
root_itemCode: "$itemsBought.itemCode"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$itemCode",
"$$root_itemCode"
]
}
}
}
],
as: "col3"
}
},
{
$addFields: {
itemsBought: {
$map: {
input: "$itemsBought",
as: "item",
in: {
"_id": "$$item._id",
"itemName": "$$item.itemName",
"itemCode": "$$item.itemCode",
"itemQuantity": {
$let: {
vars: {
input: {
$arrayElemAt: [
{
$filter: {
input: "$col2",
cond: {
$eq: [
"$$item.itemCode",
"$$this.itemCode"
]
}
}
},
0
]
},
default: "$$item.itemQuantity"
},
in: {
$ifNull: [
"$$input.itemQuantity",
"$$default"
]
}
}
},
"itemPrediction": {
$let: {
vars: {
input: {
$arrayElemAt: [
{
$filter: {
input: "$col3",
cond: {
$eq: [
"$$item.itemCode",
"$$this.itemCode"
]
}
}
},
0
]
},
default: "$$item.itemPrediction"
},
in: {
$ifNull: [
"$$input.itemPrediction",
"$$default"
]
}
}
}
}
}
}
}
},
{
$unset: [
"col2",
"col3"
]
}
])
猫鼬
当你说
更新特定字段时
是指在源集合中更改它们,还是只想在最终文档中投影不同的值?@-Joe我想在源集合中更新它@-Valijon谢谢你的帮助,通过一些小的调整,我能够从您的查询中获得所需的结果,但是它没有在集合中得到更新,我也没有得到任何更新error@sachin你能console.log
result
字段并在你的问题中发布吗?@-Valijon我已经试过了查询在shell中运行,但是在nodejs中,result或err中没有打印任何内容。我也尝试只打印一个字符串,但它不起作用,--->.exec在回调函数-->>.aggregate([…],function(err,result){console.log('result',result)}中打印结果时不起作用然后结果是打印@-Valijon我刚才试过了类型错误:item.save不是function@sachin让我们继续。再试试看
Collection1.aggregate([...], function (err, result) {
if(err) console.log("error-agg: " + err);
result.forEach(function(item) {
Collection1.updateOne({_id:item._id}, {$set:item}, function (err) {
if(err) console.log("error-saving: " + err);
});
});
});