Node.js 用于对mongoDB中具有相同密钥的2个集合中的对象进行深度复制的查询
我试图通过Mongo DB查询对来自2个集合的对象数据进行深度复制 例子: 表1数据:Node.js 用于对mongoDB中具有相同密钥的2个集合中的对象进行深度复制的查询,node.js,mongodb,nodeapi,Node.js,Mongodb,Nodeapi,我试图通过Mongo DB查询对来自2个集合的对象数据进行深度复制 例子: 表1数据: 表2数据: 所需输出 我使用$mergeObjects尝试了下面的查询,结果出现了一个错误 db.Table1.aggregate([{ $lookup: { from: "Table2", localField: "type", foreignField: "type", as: "from
表2数据:
所需输出
我使用$mergeObjects尝试了下面的查询,结果出现了一个错误
db.Table1.aggregate([{
$lookup: {
from: "Table2",
localField: "type",
foreignField: "type",
as: "fromItems"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$fromItems", 0 ] }, "$$ROOT" ] } }
},
{ $project: { fromItems: 0 } }
])
对于上述查询,产生的错误如下:
```
assert: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$mergeObjects'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1
Error: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$mergeObjects'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1```
如何在Mongo DB查询中对对象内部的对象进行深度复制
{
type:"fruits"
box1:{
apple:1,
orange:2,
grapes:3,
watermelon:4
},
box2:{
one:1,
two:2
three:3
}
}
db.Table1.aggregate([{
$lookup: {
from: "Table2",
localField: "type",
foreignField: "type",
as: "fromItems"
}
},
{
$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$fromItems", 0 ] }, "$$ROOT" ] } }
},
{ $project: { fromItems: 0 } }
])
```
assert: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$mergeObjects'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1
Error: command failed: {
"ok" : 0,
"errmsg" : "Unrecognized expression '$mergeObjects'",
"code" : 168,
"codeName" : "InvalidPipelineOperator"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:23:13
doassert@src/mongo/shell/assert.js:13:14
assert.commandWorked@src/mongo/shell/assert.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5
@(shell):1:1```