Mongodb Mongo聚合中的多分组

Mongodb Mongo聚合中的多分组,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我的mongo db数据如下 { "_id" : ObjectId("58e879052b614ce778fb7af2"), "email" : "abc@gmail.com", "phone" : 1234 } { "_id" : ObjectId("58e879052b614ce778fb7af3"), "email" : "efg@gmail.com", "phone" : 2346 } { "_id" : ObjectId("58e879052b614ce778fb7af4"), "em

我的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或没有关键电子邮件的电话号码也应包含在输出中

谢谢你

您需要和聚合命令,通过电子邮件进行分组,然后通过电话进行最终清理

无重复抑制的第一解 具有重复抑制的第二解
这似乎有效。但是我想在我的输出中也记录下{{{{}{{}id:ObjectId(“58e880f96774f83b108b4567”),“phone”:9846,“email”:“}{{{}{{}id:ObjectId(“58e881016774f83b108b4568”),“phone”:1012,“email”:“}。你好,朱利安·塔辛,你能再看看这个问题吗。我又增加了一些要求。谢谢:)你能看看语法吗。它显示了一些意外的令牌错误。很抱歉再次出现问题。感谢您更正语法:)对不起,还有一个条件。因为电话号码1234和5789是电子邮件id的一部分abc@gmail.com,则不需要将其包含在输出中,而使用空电子邮件值。请您也检查一下。对不起,我在第二个解决方案中再次遇到一些错误,重复抑制为“errmsg”:“异常:$unwind字段路径必须指定为字符串”,“代码”:15981。不知道为什么。
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" }