将SQL转换为pymongo查询

时间:2013-08-22 05:55:32

标签: python mongodb pymongo

文档原型:

{
    "d": "D", 
    "g": {
        "c": "C", 
        "a": "A", 
        "b": "B"
    }, 
    "e": "E", 
    "f": "F"
}

相当于:

SELECT a, b, c, d from Table WHERE d='D' AND e='E' GROUP BY a
在使用pymongo的mongodb中

以下查询返回对象:

db.<collection>.find({'d': 'D'}, {'g.c': 1, 'g.a': 1, 'g.b': 1, 'd': 1, '_id': 0})

但是,以下情况并非如此:

db.<collection>.aggregate([{$match:{"d":"D", "e":"E"}},
                           {$group:{_id:"$g.a"}}])

它返回一个空列表,甚至不是查询(游标)对象。

另外,如何将$ project包含在其中,以便我可以将输出限制为a,b,c d的字段?

注意我已经创建了集合,以便过滤e ='E'。

2 个答案:

答案 0 :(得分:2)

实际上您的查询不是有效的SQL。

你必须引用字符串

SELECT a, b, c, d
from Table
WHERE d='D' AND e='E'
GROUP BY a

此查询仍然只适用于MySQL。对于ANSI SQL(以及大多数实现),您应该为列指定聚合,例如

SELECT a, min(b) as b, max(c) as c
from Table
WHERE d='D' AND e='E'
GROUP BY a

然后你的mongodb查询就像

db.<your collection>.aggregate([
   {$match:{"d":"D", "e":"E"}},
   {$group:{_id:"$g.a", b: { $min: "$g.b"}, c: {$max:"$g.c"}}}
])

如果你想要一个a,b,c,d数组的数组,这应该有效:

db.<your collection>.aggregate([
   {$match:{"d": "D", "e": "E"}},
   {
       $group: {
            _id: "$g.a",
            data: {$push: {"a": "$g.a", "b": "$g.b", "c": "$g.c", "d": "$d"}}
       }
   }
])

刚刚测试了这段代码 - 它的工作原理,这里是python代码:

>>> cl = MongoClient()
>>> coll = cl["local"]["test3"]
>>> res = coll.aggregate([{"$match":{"d": "D", "e": "E"}},{"$group":{"_id":"$g.a", "data": {"$push":{"a":"$g.a", "b":"$g.b", "c":"$g.c", "d":"$d"}}}}])
>>> res["result"]
[{'_id': 'A', 'data': [{'a': 'A', 'c': 'C', 'b': 'B', 'd': 'D'}, {'a': 'A', 'c': 'K', 'b': u'V', 'd': 'D'}]}]

答案 1 :(得分:0)

正如你所看到的,@ RomanPekar的答案确实有效:

> db.z.insert({d:'D',g:{c:'C',a:'A',b:'B'},e:'E',f:'F'})
> db.z.aggregate([{$match:{d:'D',e:'E'}},{$group:{_id:'$g.a'}}])
{ "result" : [ { "_id" : "A" } ], "ok" : 1 }

问题是,很可能是你试图在python中这样做而不改变语法。相反,你会在python中运行它:

db.z.aggregate([{"$match":{"d":"D","e":"E"}},{"$group":{"_id":"$g.a"}}])

供参考:http://api.mongodb.org/python/current/examples/aggregation.html