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?
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 ,c d only?
Note I 've created the collection so as to filter e='E'.
> 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 }Also that is not valid pymongo syntax