如果我在mongodb集合中有多个文档,如下所示:
// document 1
{
_id: '123',
date: '5/10/15',
charges: [{
amount: 500,
description: 'foo',
},{
amount: 400,
description: 'bar',
}],
}
// document 2
{
_id: '456',
date: '5/11/15',
charges: [{
amount: 500,
description: 'foo',
},{
amount: 300,
description: 'foo',
}],
}
我想创建数量为500的所有费用和数组。结果应如下所示:
[{
amount: 500,
description: 'foo'
}, {
amount: 500,
description: 'foo'
}]
实现这一目标的最有效方法是什么?
答案 0 :(得分:2)
试试这个:
db.collection.aggregate(
[
{
$unwind: "$charges"
},
{
$match: {
amount: 500
}
}
]
);