Find的条件,Mongo

时间:2015-07-17 16:14:01

标签: python mongodb mongodb-query pymongo aggregation-framework

我有一个带有doc的mongo集合,如下所示: -

{
    "_id" : ObjectId("55a9378ee2874f0ed7b7cb7e"),
    "_uid" : 10,
    "impressions" : [
            {
                    "pos" : 6,
                    "id" : 123,
                    "service" : "furniture"
            },
            {
                    "pos" : 0,
                    "id" : 128,
                    "service" : "electronics"
            },
            {
                    "pos" : 2,
                    "id" : 127,
                    "service" : "furniture"
            },
            {
                    "pos" : 2,
                    "id" : 125,
                    "service" : "electronics"
            },
            {
                    "pos" : 10,
                    "id" : 124,
                    "service" : "electronics"
            }
    ]
},
{
    "_id" : ObjectId("55a9378ee2874f0ed7b7cb7f"),
    "_uid" : 11,
    "impressions" : [
            {
                    "pos" : 1,
                    "id" : 124,
                    "service" : "furniture"
            },
            {
                    "pos" : 10,
                    "id" : 124,
                    "service" : "electronics"
            },
            {
                    "pos" : 1,
                    "id" : 123,
                    "service" : "furniture"
            },
            {
                    "pos" : 21,
                    "id" : 122,
                    "service" : "furniture"
            },
            {
                    "pos" : 3,
                    "id" : 125,
                    "service" : "electronics"
            },
            {
                    "pos" : 10,
                    "id" : 121,
                    "service" : "electronics"
            }
    ]
}

我的目标是找到特定"id"中的所有"service""furniture",即得到这样的结果:

[122,123,124,127]

但是我无法弄清楚如何在

中构建条件
db.collection_name.find()

因为难以为“&n”提供条件。数组中的元素"impressions[n]":"value"

一种选择是使用"id"获得的执行汇总操作来查找服务的每个"id"的展示次数,这是我之前提到的这个问题的答案所建议的: - MapReduce in PyMongo

但我只希望服务中的不同'id'列表不是展示次数。 请帮助!

1 个答案:

答案 0 :(得分:1)

您需要aggregration framework才能获得有意义的结果。非常喜欢这样:

result = db.collection.aggregate([
    { "$match": {
        "impressions.service": "furniture"
    }},
    { "$unwind": "$impressions" },
    { "$match": {
        "impressions.service": "furniture"
    }},
    { "$group": {
        "_id": "$impressions.id"
    }}
])

或者更好的是使用MongoDB 2.6或更高版本,可以使用$unwind删除$redact之前无法匹配的数组项:

result = db.collection.aggregate([
    { "$match": {
        "impressions.service": "furniture"
    }},
    { "$redact": {
       "$cond": {
           "if": { 
               "$eq": [
                   { "$ifNull": [ "$service", "furniture" ] },
                   "furniture"
               ]
           },
           "then": "$$DESCEND",
           "else": "$$PRUNE"
       }
    }},
    { "$unwind": "$impressions" },
    { "$group": {
        "_id": "$impressions.id"
    }}
])

哪个收益率:

{ "_id" : 122 }
{ "_id" : 124 }
{ "_id" : 127 }
{ "_id" : 123 }

不是简单的“列表”,而是转换它,因此:

def mapper (x):
    return x["_id"]

map(mapper,result)

或者:

map(lambda x: x["_id"], result)

给你:

[122, 124, 127, 123]

如果您希望它“排序”,则在聚合管道的末尾添加$sort阶段或在代码中对结果列表进行排序。