【发布时间】:2022-01-21 11:45:41
【问题描述】:
例如,我在这样的集合中有数据:
db.inventory2.insertMany([
{ "_id" : 1, "item" : "ABC", price: NumberDecimal("80"), "sizes": [ "S", "M", "L"] },
{ "_id" : 2, "item" : "EFG", price: NumberDecimal("120"), "sizes" : [ ] },
{ "_id" : 3, "item" : "IJK", price: NumberDecimal("160"), "sizes": ["M"] },
])
并且,在添加 $unwind 时,我想选择 size 不为 null 的项目。
我添加 $unwind 后的结果:
db.inventory2.aggregate( [ { $unwind: { path: "$sizes" } } ] )
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : "S" }
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : "M" }
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : "L" }
{ "_id" : 3, "item" : "IJK", "price" : NumberDecimal("160"), "sizes" : "M" }
我的问题是,当我添加 $unwind 时,如何不分隔结果项“ABC”。我的预期结果是这样的:
{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("80"), "sizes" : ["S", "M", "L"] }
{ "_id" : 3, "item" : "IJK", "price" : NumberDecimal("160"), "sizes" : ["M"] }
感谢您的帮助。
【问题讨论】:
标签: node.js mongodb aggregation-framework