你可以很容易地使用聚合框架来做到这一点:
db.so.aggregate( [
{ $unwind: "$images" },
{ $group: {
_id: '$_id',
all: { $sum: 1 },
all_primary: { $sum: { $cond: [ { $eq: [ '$images.primary', true ] }, 1, 0 ] } },
images: { $push: '$images' },
name: { $first: '$name' },
etc: { $first: '$etc' },
} },
{ $project: {
_id: 1,
images: 1,
name: 1,
etc: 1,
same: { $cond: [ { $eq: [ '$all', '$all_primary' ] }, 1, 0 ] }
} },
{ $match: { 'same' : 1 } }
] );
以此作为输入:
{
"_id" : ObjectId("5203730bf8eaa52a846ebc3e"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000010"),
"primary" : true
}
],
"name" : "Derick",
"Etc" : true
}
{
"_id" : ObjectId("52037315f8eaa52a846ebc3f"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : false
}
],
"name" : "James",
"Etc" : true
}
{
"_id" : ObjectId("520373621a78238235b6ffbf"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : false
}
],
"name" : "James",
"etc" : true
}
{
"_id" : ObjectId("5203736b1a78238235b6ffc0"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : true
}
],
"name" : "James",
"etc" : true
}
这输出:
{
"result" : [
{
"_id" : ObjectId("5203736b1a78238235b6ffc0"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000020"),
"primary" : true
}
],
"name" : "James",
"etc" : true,
"same" : 1
},
{
"_id" : ObjectId("5203730bf8eaa52a846ebc3e"),
"images" : [
{
"id" : ObjectId("516bef7fc05e877b31000000"),
"primary" : true
},
{
"id" : ObjectId("516bef2ac05e879622010000"),
"primary" : true
},
{
"id" : ObjectId("516beeb7c05e879e2a000010"),
"primary" : true
}
],
"name" : "Derick",
"etc" : null,
"same" : 1
}
],
"ok" : 1
}