我的mongo db数据就像
{ "_id" : ObjectId("58e879052b614ce778fb7af2"), "email" : "abc@gmail.com", "phone" : 1234 }
{ "_id" : ObjectId("58e879052b614ce778fb7af3"), "email" : "efg@gmail.com", "phone" : 2346 }
{ "_id" : ObjectId("58e879052b614ce778fb7af4"), "email" : "abc@gmail.com", "phone" : 7896 }
{ "_id" : ObjectId("58e879052b614ce778fb7af5"), "phone" : 5789, "email" : "abc@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af6"), "phone" : 7896, "email" : "hij@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af7"), "phone" : 3492, "email" : "lmn@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af8"), "phone" : 5555, "email" : "cdf@gmail.com" }
{ "_id" : ObjectId("58e87ea96774f88e108b4567"), "phone" : 5789, "email" : "abc@gmail.com" }
{ "_id" : ObjectId("58e880db6774f8130f8b4567"), "phone" : 5789, "email" : "" }
{ "_id" : ObjectId("58e880e56774f81f108b4567"), "phone" : 1234, "email" : "" }
{ "_id" : ObjectId("58e880f96774f83b108b4567"), "phone" : 9846, "email" : "" }
{ "_id" : ObjectId("58e881016774f83b108b4568"), "phone" : 1012, "email" : "" }
{ "_id" : ObjectId("58e8812a6774f8c0108b4567"), "phone" : 1258 }
{ "_id" : ObjectId("58e881496774f80e108b4567"), "phone" : 1012, "email" : "" }
我想首先根据电子邮件分组,然后使用手机。
{ "_id" : ObjectId("58e879052b614ce778fb7af2"), "email" : "abc@gmail.com", "phone" : 1234 }
{ "_id" : ObjectId("58e879052b614ce778fb7af3"), "email" : "efg@gmail.com", "phone" : 2346 }
{ "_id" : ObjectId("58e879052b614ce778fb7af6"), "phone" : 7896, "email" : "hij@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af7"), "phone" : 3492, "email" : "lmn@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af8"), "phone" : 5555, "email" : "cdf@gmail.com" }
{ "_id" : ObjectId("58e880f96774f83b108b4567"), "phone" : 9846, "email" : "" }
{ "_id" : ObjectId("58e881016774f83b108b4568"), "phone" : 1012, "email" : "" }
{ "_id" : ObjectId("58e8812a6774f8c0108b4567"), "phone" : 1258 }
我想要除了空值以外的所有电子邮件ID以及相应的电话号码。还希望电话号码不具有与群组电子邮件相同的电子邮件ID。具有空电子邮件ID或没有密钥电子邮件的电话号码也应包含在输出中。
感谢你。
答案 0 :(得分:1)
您需要$group和$first汇总命令,要通过电子邮件进行分组,然后致电,您必须使用$cond与最终$project进行清理。
db.data.aggregate([ {$group: {_id: {$cond: {if: {"$eq": ['$email', '']}, else: {email: '$email'}, then: {phone: '$phone', email: '$email'}}}, firstId: {$first: '$_id'}, phone: {$first: '$phone'}}}, {$project: {_id: '$firstId', phone: '$phone', email: '$_id.email'}} ])
{ "_id" : ObjectId("58e880f96774f83b108b4567"), "phone" : 9846, "email" : "" }
{ "_id" : ObjectId("58e881016774f83b108b4568"), "phone" : 1012, "email" : "" }
{ "_id" : ObjectId("58e880e56774f81f108b4567"), "phone" : 1234, "email" : "" }
{ "_id" : ObjectId("58e880db6774f8130f8b4567"), "phone" : 5789, "email" : "" }
{ "_id" : ObjectId("58e879052b614ce778fb7af8"), "phone" : 5555, "email" : "cdf@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af6"), "phone" : 7896, "email" : "hij@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af7"), "phone" : 3492, "email" : "lmn@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af3"), "phone" : 2346, "email" : "efg@gmail.com" }
{ "_id" : ObjectId("58e8812a6774f8c0108b4567"), "phone" : 1258 }
{ "_id" : ObjectId("58e879052b614ce778fb7af2"), "phone" : 1234, "email" : "abc@gmail.com" }
db.data.aggregate([
{$group: {_id: '$phone', emails: {$addToSet: {email: '$email', _id: '$_id'}}, countNonBlank: {$sum: {$cond: [{$eq: ['$email', '']}, 0, 1]}}}}, {$unwind: {path: '$emails', preserveNullAndEmptyArrays: true}},
{$match: {$or: [{'emails.email': {$ne: ''}}, {"countNonBlank": {$lt: 1}, "emails.email": {$eq: ''}}]}},
{$project: {phone: '$_id', email: '$emails.email', _id: '$emails._id'}},
{$group: {_id: {email: '$email', phone: '$phone'}, firstId: {$first: '$_id'}}},
{$project: {_id: '$firstId', phone: '$_id.phone', email: '$_id.email'}},
{$group: {_id: {$cond: {if: {"$eq": ['$email', '']}, else: {email: '$email'}, then: {phone: '$phone', email: '$email'}}}, firstId: {$first: '$_id'}, phone: {$first: '$phone'}}},
{$project: {_id: '$firstId', phone: '$phone', email: '$_id.email'}}
])
{ "_id" : ObjectId("58e881496774f80e108b4567"), "phone" : 1012, "email" : "" }
{ "_id" : ObjectId("58e880f96774f83b108b4567"), "phone" : 9846, "email" : "" }
{ "_id" : ObjectId("58e879052b614ce778fb7af3"), "phone" : 2346, "email" : "efg@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af7"), "phone" : 3492, "email" : "lmn@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af6"), "phone" : 7896, "email" : "hij@gmail.com" }
{ "_id" : ObjectId("58e879052b614ce778fb7af8"), "phone" : 5555, "email" : "cdf@gmail.com" }
{ "_id" : ObjectId("58e8812a6774f8c0108b4567"), "phone" : 1258 }
{ "_id" : ObjectId("58e879052b614ce778fb7af2"), "phone" : 1234, "email" : "abc@gmail.com" }