I'm trying to use aggregate with mongoengine. Also, it was challenging to find out, how this works (because there is nothing about it in the documentation), I've made a little progress.
But I can't match a primary key:
from mongoengine import *
connect('test')
class User(Document):
username = StringField()
def match_test_username(self):
pipeline = [{ "$match": {"username": self.username} }]
return User.objects.aggregate(*pipeline)
def match_test_id(self):
pipeline = [{ "$match": {"id": self.id} }]
return User.objects.aggregate(*pipeline)
mary = User(username="mary")
mary.save()
mary.reload()
agg_username = mary.match_test_username
for doc in agg_username():
print("match_test_username:", doc)
agg_id = mary.match_test_id
for doc in agg_id():
print("match_test_id:", doc)
If you run this, matching is working for the username (match_test_username), but I can't get it to work with the primary key, I've tried key name: _id, pk, id.
What do I wrong?
And is there any Documentation about this? All I found is this: Flask-MongoEngine & PyMongo Aggregation Query what was very helpful.
What also would help, is there a better way ("pythonic way") to iterate over the findings?
Thank you in advance!