Попробуйте:
db.shop.aggregate([{
$group: {
_id: '', shop: { $addToSet: '$shop' },
emptyBatchShops: { $push: { $cond: [{ $eq: ['$batch', []] }, '$shop', null] } }
}
}, { $project: { _id :0, uniques: { $setDifference: ["$shop", "$emptyBatchShops"] } } }])
Данные сбора:
/* 1 */
{
"_id" : ObjectId("5e33c4daa855df1587760621"),
"shop" : "Q4",
"batch" : [
"5647"
]
}
/* 2 */
{
"_id" : ObjectId("5e33c4daa855df1587760622"),
"shop" : "Q4",
"batch" : [
"2314",
"0121"
]
}
/* 3 */
{
"_id" : ObjectId("5e33c4daa855df1587760623"),
"shop" : "Q1",
"batch" : [
"5647"
]
}
/* 4 */
{
"_id" : ObjectId("5e33c4daa855df1587760624"),
"shop" : "Q1",
"batch" : []
}
/* 5 */
{
"_id" : ObjectId("5e33c8fca855df1587760625"),
"shop" : "Q5",
"batch" : [
"5647"
]
}
/* 6 */
{
"_id" : ObjectId("5e33c8fca855df1587760626"),
"shop" : "Q5",
"batch" : [
"5647"
]
}
/* 7 */
{
"_id" : ObjectId("5e33c907a855df1587760627"),
"shop" : "Q6",
"batch" : []
}
/* 8 */
{
"_id" : ObjectId("5e33c907a855df1587760628"),
"shop" : "Q6",
"batch" : [
"5647"
]
}
/* 9 */
{
"_id" : ObjectId("5e33c90ea855df1587760629"),
"shop" : "Q7",
"batch" : []
}
/* 10 */
{
"_id" : ObjectId("5e33c90ea855df158776062a"),
"shop" : "Q8",
"batch" : [
"5647"
]
}
Результат:
{
"uniques" : [
"Q5",
"Q4",
"Q8"
]
}