如何在mongoengine python中聚合组
作者:互联网
我正在尝试在MongoDB中汇总组.
我们正在使用Python,django,Rest框架和Mongoengine.
这是我的模型的样子:
class TestSnapshot(EmbeddedDocument):
identifier = StringField()
name = StringField()
created = DateTimeField(
default = datetime.datetime.now)
class Test(Document):
agreement = ReferenceField(Agreement)
history = ListField(EmbeddedDocumentField(TestSnapshot))
created = DateTimeField(
default = datetime.datetime.now)
在我看来:
test = models.Test.objects.filter(agreement__in = agreements).aggregate([{
"$unwind": "$created"
}, {
"$group": {
"_id": "$created"
}
}])
print test
但是我有这样的引用:
OperationFailure: command SON([('aggregate', u 'test'), ('pipeline', [{
'$match': {
'agreement': {
'$in': [ObjectId('558b150a89f4ad3f33ebb8ae')]
}
}
},
[{
'$unwind': '$created'
}, {
'$group': {
'_id': '$created'
}
}]
]), ('cursor', {})]) on namespace noq.$cmd failed: exception: pipeline element 1 is not an object
我究竟做错了什么?我是mongo的Python新手.
感谢您的帮助:)
最好的祝福,
强尼
解决方法:
我已经解决了问题,但我做错了.
models.Test.objects.filter(agreement__in = agreements).aggregate(
{"$group": { "_id": "$created" }}
)
不管怎么说,还是要谢谢你 :)
标签:mongodb,python,django,mongoengine 来源: https://codeday.me/bug/20191120/2041283.html