如果其他项不为null,则在集合中获取不同的项

时间:2013-07-02 11:02:40

标签: mongodb pymongo

我有一个像这样的集合

{
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 = ''

1 个答案:

答案 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"}}}
])