我有一个带有嵌入式文档列表的Mongo文档(下面的示例)。我正在尝试运行一个聚合,该聚合将返回列表中仅包含匹配对象的完整文档。
示例收集数据
{
"make": "Toyota",
"color": "blue",
"tires": [{
"make": "Mishlen",
"size": 185
}, {
"make": "Mishlen",
"size": 210
}]
}
我设法通过以下查询使其在MongoDB中工作
db.cars.aggregate(
[
{
$match: {$and: [{"tires.size": {$gt: 200}}]}
},
{
$addFields: {
"tires": {
$filter: {
input: '$tires',
as: 'tires',
cond: {$gt: ['$$tires.size', 200]}
}
}
}
},
{
$limit: 100
},
{
$skip: 0
}
])
我正在尝试在mongoengine中运行相同的聚合,并且每次都返回一个空列表。
pipeline = [
{
"$match": {"$and": [{"tires.size": {"$gt": 200}}]}
},
{
"$addFields": {
"tires": {
"$filter": {
"input": "$tires",
"as": "tires",
"cond": {"$and": [{"$gt": ["$$tires.size", 200]}]}
}
}
}
}
]
self.obj_type.objects.aggregate(*pipeline)
我究竟做错了什么?
更新
我的问题比我想象的要容易得多,我想念我将数字作为字符串而不是python中的int传递。谢谢大家的帮助
插入您提供的示例文档后,使用您提供的管道运行聚合没有任何特殊问题。见下文:
class Tire(EmbeddedDocument):
make = StringField()
size = IntField()
class Car(Document):
make = StringField()
color = StringField()
tires = EmbeddedDocumentListField(Tire)
meta = {'collection': 'cars'}
pipeline = [
{
"$match": {"$and": [{"tires.size": {"$gt": 200}}]}
},
{
"$addFields": {
"tires": {
"$filter": {
"input": "$tires",
"as": "tires",
"cond": {"$and": [{"$gt": ["$$tires.size", 200]}]}
}
}
}
}
]
# Verify aggregation pipeline runs fine with the driver (pymongo)
coll = Car._get_collection()
pymongo_result = list(coll.aggregate(pipeline))
assert len(pymongo_result) == 1
# Run same aggregation pipeline with MongoEngine
mongoengine_result = list(Car.objects.aggregate(*pipeline))
assert len(mongoengine_result) == 1
result = [
{'_id': ObjectId('5e0a5c8e7c57cd9b300710fb'),
'color': 'blue',
'make': 'Toyota',
'tires': [{'make': 'Mishlen', 'size': 210.0}]
}
]
assert mongoengine_result == pymongo_result == result
我使用了最新的mongoengine版本,但AFAIK最近没有对MongoEngine的聚合包装进行重大更改。问题可能出self.obj_type.objects
在您的头上,请像我一样尝试使用新的查询集(即YourDocumentClass.objects
),看看是否有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句