【发布时间】:2026-01-03 10:55:01
【问题描述】:
我正在使用 mongodb 3.6,其中我有 Usermst 集合,其中包含用户文档。我将获取发布更多帖子的用户名字和姓氏。下面是我的 mongodb 查询。
db.getCollection("UserMst").aggregate([
{$match :{$and:[{os : {$in:[0,1]}}, {_id : {$nin : [3,10]}}]}}
,{$match:{$and:
[ {$or: [
{$and : [{fname:{$regex : `^has.*` , $options: 'i' }},{lname:{$regex : `^pa.*` , $options: 'i' }}]}
,{$and: [{fname:{$regex : `^pa.*` , $options: 'i' }}, {lname:{$regex : `^has.*` , $options: 'i' }}]}
]}
]
}
}
,{$sort:{'posts':-1,'_id':-1}}
,{$project:{"fname":1,"lname":1,"posts":1}}
,{$limit:5}
])
我有索引“名称”:“os_1_posts_-1”。这个查询很费时间。有什么办法可以优化查询吗?
【问题讨论】:
-
posts是一个数组吗? -
可以提供文件样本吗?
-
@willis,没有帖子有整数值。
-
@matthPen 下面是示例文档。 {“_id”:NumberInt(8596321),“fname”:“Harth”,“lname”:“jose”,“imgpath”:“”,“imgname”:“”,“status”:“A”,“posts " : 7.0, "os" : 1.0 }
标签: regex mongodb performance sorting aggregation-framework