我正在尝试基于嵌套的嵌套数组中的值更新文档中的字段(totalPrice)(addOns>从x个数组中获取匹配数组>在匹配数组中获取int [总是在pos 2])。
这是一个在addOns中嵌套2个数组的文档示例:
{
"_id": ObjectID('.....'),
"addOns": [
["appleId", "Apples", 2],
["bananaID", "Bananas", 1]
],
"totalPrice": 5.7
}
比方说,香蕉的价格上涨了20美分,因此我需要查找其附件中包含香蕉的所有文档,然后根据所购买的香蕉的数量来增加这些文档的totalPrice。我计划使用一个聚合管道使用一个updateMany()查询,该管道应该大致类似于以下示例。 ???应该是购买的香蕉数量,但是我不确定如何去获取该价值。到目前为止,我已经考虑过使用$ unwind,$ filter和$ arrayElemAt,但是不确定如何将它们一起使用。不胜感激!
db.collection('orders').updateMany(
{ $elemMatch: { $elemMatch: { $in: ['bananaID'] } } },
[
{ $set: {'totalPrice': { $add: ['$totalPrice', { $multiply: [{$toDecimal: '0.2'}, ???] } ] } } }
]
我不确定我的mongo版本是什么,但我确实知道我可以使用聚合管道,因为我还有其他的updateMany()调用也使用该管道而没有任何问题,因此应该是(version> = 4.2)。
** 编辑:考虑到???,过滤步骤似乎在文档更新时有些起作用,但该值为null而不是更新的价格。不知道为什么
{ $arrayElemAt: [{ $arrayElemAt: [ { $filter: { input: "$addOns", as:'this', cond: { $eq: ['$$this.0', 'bananaID'] } } } , 0 ] }, 2 ] }
答案 0 :(得分:0)
设法自己解决-尽管仅在不介意Joe在问题评论中指出的updateMany()风险的情况下才使用它。它与我最初在** Edit 中共享的内容非常相似,不同之处在于您无法使用$$this.0
访问数组中的元素。
将此内容插入???是并且它将起作用,在此代码块下方是解释:
{ $arrayElemAt: [{ $arrayElemAt: [ { $filter: { input: "$addOns", as:'this', cond: { $eq: [{$arrayElemAt:['$$this', 0]}, 'bananaID'] } } } , 0 ] }, 2 ] }
[["appleId", "Apples", 2], ["bananaID", "Bananas", 1]]
$$this
代表输入中的每个元素,我们检查$$this
的第一个元素是否与'bananaID'相匹配。{ $filter: { input: "$addOns", as:'this', cond: { $eq: [{$arrayElemAt:['$$this', 0]}, 'bananaID'] } } }
// how the result from $filter should look like
// [["bananaID", "Bananas", 1]]
// How it looks like after step 3: ["bananaID", "Bananas", 1]
db.collection('orders').updateMany(
{ $elemMatch: { $elemMatch: { $in: ['bananaID'] } } },
[
{ $set: {'totalPrice': { $add: ['$totalPrice', { $multiply: [{$toDecimal: '0.2'}, 1] } ] } } }
], *callback function code*)
// notice how the ??? is now replaced by the quantity of bananas which is 1