[英]mongodb command very slow
I have 3 documents like this:我有 3 个这样的文件:
{
_id: ObjectId("..."),
_details: {
_session: ObjectId("example_1"),
},
{
_id: ObjectId("..."),
_details: {
_session: ObjectId("example_1"),
},
{
_id: ObjectId("..."),
_details: {
_session: ObjectId("example_2"),
}
And I'm trying to retrieve and group the _details._session
's ids.我正在尝试检索和分组
_details._session
的 ID。 Expected output for the above example dataset would be:上述示例数据集的预期输出为:
['example_1', 'example_2']
I have tried the following Python script:我尝试了以下 Python 脚本:
cursor = mycol.find({}, {"_details.session": 1})
sessions = []
for doc in cursor:
if doc['_details']['_session'] not in sessions:
sessions.append(doc['_details']['_session'])
Problem is that it takes around 1 minute for 500 documents.问题是 500 个文档大约需要 1 分钟。
Is there any way to speed up that command?有什么方法可以加快该命令的速度吗? I need it to run the fastest way possible.
我需要它以最快的方式运行。
db.collection.aggregate([
{
"$group": {
"_id": null,
"uniqueSessions": {
"$addToSet": "$_details._session"
}
}
}
])
You don't need to iterate through each document.您不需要遍历每个文档。 You can achieve many things easily and efficiently using mongo aggregation framework.
您可以使用 mongo 聚合框架轻松高效地完成许多事情。
You can add a $project
stage to avoid _id:null
in the output if it really bothers.您可以添加一个
$project
阶段以避免在输出中出现_id:null
如果它真的很麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.