简体   繁体   English

将SQL转换为pymongo查询

[英]Convert an SQL into pymongo query

Document prototype: 文件原型:

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

What would be the equivalent of: 相当于什么:

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

in mongodb using pymongo? 在mongodb中使用pymongo?

The following query returns objects: 以下查询返回对象:

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

But, the following doesn't: 但是,以下不是:

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

It returns an empty list, not even a query(cursor) object. 它返回一个空列表,甚至没有查询(游标)对象。

Also, how could I include $project into it so that I may restrict the output to the fields of a, b ,cd only? 另外,如何将$ project包含在其中,以便将输出限制在a,b和cd的字段中?

Note I 've created the collection so as to filter e='E'. 注意我已经创建了集合,以便过滤e ='E'。

Actually your query is not a valid SQL. 实际上,您的查询不是有效的SQL。

You have to quote strings 您必须引用字符串

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

And still this query will work only in MySQL. 而且此查询仅在MySQL中有效。 For ANSI SQL (and most implementations) you should specify aggregates for your columns, like 对于ANSI SQL(以及大多数实现),您应为列指定汇总,例如

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

then your mongodb query would be like 那么您的mongodb查询将是

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

if you want an array of a, b, c, d values, this should work: 如果您想要一个由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"}}
       }
   }
])

Just tested this code - it works, here's python code: 刚刚测试过此代码-它起作用,这是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'}]}]

As you can see the answer by @RomanPekar does actually work: 如您所见,@ 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 }

The problem is, most likely, that you are trying to do this in python without changing the syntax. 问题很可能是您尝试在python中执行此操作而不更改语法。 Instead you would run this in python: 相反,您可以在python中运行它:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM