python - 按一个键的值排序 mongodb
python - sort mongodb by the value of one key
我有一个具有以下数据结构的集合:
[{name: "123", category: "A"},
{name: "456", category: "B"},
{name: "789", category: "A"},
{name: "101", category: "C"}]
我希望能够根据 category
的值对它们进行排序,方法是指定哪个先出现。例如,按照 B->C->A 的顺序对查询进行排序,结果将产生:
[{name: "456", category: "B"},
{name: "101", category: "C"},
{name: "123", category: "A"},
{name: "789", category: "A"}]
使用 mongo 查询 API 有什么好的方法吗?我正在使用 mongoengine
我觉得MongoDB还不能提供自定义排序功能:
不过,作为解决方法,您可以按 Python 排序,将类别映射到实际用于对文档进行排序的数值:
from pprint import pprint
weights = {
"A": 2,
"B": 0,
"C": 1
}
docs = db.col.find()
pprint(sorted(docs, key=lambda item: weights.get(item["category"])))
最好的方法是使用 .aggregate()
method and the $cond
conditional operator to add a weight to your documents in the $project
stage then use the $sort
聚合运算符按 权重.
对文档进行排序
pipeline = [{'$project': {'category': 1,
'name': 1,
'w': {'$cond': [{'$eq': ['$category', 'B']},
1,
{'$cond': [{'$eq': ['$category', 'C']},
2,
3]
}]
}
}},
{'$sort': {'w': 1}}
]
Model.aggregate(*pipeline)
使用 PyMongo 的演示:
>>> import pprint
>>> import pymongo
>>> client = pymongo.MongoClient()
>>> collection = client['test']['collection']
>>> pipeline = [{'$project': {'category': 1,
... 'name': 1,
... 'w': {'$cond': [{'$eq': ['$category', 'B']},
... 1,
... {'$cond': [{'$eq': ['$category', 'C']},
... 2,
... 3]
... }]
... }
... }},
... {'$sort': {'w': 1}}
... ]
>>> pprint.pprint(list(collection.aggregate(pipeline=pipeline)))
[{'_id': ObjectId('571caa930e4f55302502a361'),
'category': 'B',
'name': '456',
'w': 1},
{'_id': ObjectId('571caa930e4f55302502a363'),
'category': 'C',
'name': '101',
'w': 2},
{'_id': ObjectId('571caa930e4f55302502a360'),
'category': 'A',
'name': '123',
'w': 3},
{'_id': ObjectId('571caa930e4f55302502a362'),
'category': 'A',
'name': '789',
'w': 3}]
我有一个具有以下数据结构的集合:
[{name: "123", category: "A"},
{name: "456", category: "B"},
{name: "789", category: "A"},
{name: "101", category: "C"}]
我希望能够根据 category
的值对它们进行排序,方法是指定哪个先出现。例如,按照 B->C->A 的顺序对查询进行排序,结果将产生:
[{name: "456", category: "B"},
{name: "101", category: "C"},
{name: "123", category: "A"},
{name: "789", category: "A"}]
使用 mongo 查询 API 有什么好的方法吗?我正在使用 mongoengine
我觉得MongoDB还不能提供自定义排序功能:
不过,作为解决方法,您可以按 Python 排序,将类别映射到实际用于对文档进行排序的数值:
from pprint import pprint
weights = {
"A": 2,
"B": 0,
"C": 1
}
docs = db.col.find()
pprint(sorted(docs, key=lambda item: weights.get(item["category"])))
最好的方法是使用 .aggregate()
method and the $cond
conditional operator to add a weight to your documents in the $project
stage then use the $sort
聚合运算符按 权重.
pipeline = [{'$project': {'category': 1,
'name': 1,
'w': {'$cond': [{'$eq': ['$category', 'B']},
1,
{'$cond': [{'$eq': ['$category', 'C']},
2,
3]
}]
}
}},
{'$sort': {'w': 1}}
]
Model.aggregate(*pipeline)
使用 PyMongo 的演示:
>>> import pprint
>>> import pymongo
>>> client = pymongo.MongoClient()
>>> collection = client['test']['collection']
>>> pipeline = [{'$project': {'category': 1,
... 'name': 1,
... 'w': {'$cond': [{'$eq': ['$category', 'B']},
... 1,
... {'$cond': [{'$eq': ['$category', 'C']},
... 2,
... 3]
... }]
... }
... }},
... {'$sort': {'w': 1}}
... ]
>>> pprint.pprint(list(collection.aggregate(pipeline=pipeline)))
[{'_id': ObjectId('571caa930e4f55302502a361'),
'category': 'B',
'name': '456',
'w': 1},
{'_id': ObjectId('571caa930e4f55302502a363'),
'category': 'C',
'name': '101',
'w': 2},
{'_id': ObjectId('571caa930e4f55302502a360'),
'category': 'A',
'name': '123',
'w': 3},
{'_id': ObjectId('571caa930e4f55302502a362'),
'category': 'A',
'name': '789',
'w': 3}]