我有一个像这样的集合
{
Country : 'XYZ'
Books : [
{"name" : "book1", "url" : "book1url", "auth_email" : "emailid1"},
{"name" : "book2", "url" : "book2url", "auth_email" : "emailid2"},
{"name" : "book3", "url" : "book3url", "auth_email" : "emailid3"},
{"name" : "book4", "url" : "book4url", "auth_email" : "emailid4"}
..........................................
]
}
我想仅在distinct 'Books.name' and corresponding 'Books.email'
'Books.email' is not = ''
答案 0 :(得分:1)
不幸的是,我现在能够提出的最简单方法是展开Books
数组,只匹配那些具有电子邮件价值的数据,然后再备份根文档:
db.collection.aggregate([
{$unwind:'$Books'},
{$match: {'Books.auth_email':{$nin:['',null]}}},
{$group: {_id:'$Books.name',email:{$first:'$Books.auth_email'}}}
])
虽然另一种方式也可以起作用:
db.collection.aggregate([
{$unwind:'$Books'},
{$match: {'Books.auth_email':{$nin:['',null]}}},
{$group: {_id:'$_id',Country:'$Country',Books:{$addToSet:'$Books'}}}
])
我相信在python中你可以做到:
self.mongo.aggregate([
{"$unwind":"$Books"},
{"$match": {"Books.auth_email":{"$nin":['',null]}}},
{"$group": {"_id":"$Books.name","email":{"$first":"$Books.auth_email"}}}
])