Reputation: 77
1st of all sorry for my english.
For now my project have 1 million chat record.
So I want to query Chats
collection from mongodb but It's take time to query and some time take 100% of cpu. So I want to know how to optimize query.
After I research maybe It should use index
and lean()
but I'm not clear how to using this with my data.
Here my Chat
data look like
{
"_id" : ObjectId("602c53cad1dc22359729bb62"),
"textMessage" : "1",
"imageNames" : [],
"uuid" : "a9y10mfqtokl8mtk0l",
"sendBy" : "a9y10m9s2skkwu564g",
"sendTo" : "a9y10m9s2skkwvo7xl",
"createDateTimestamp" : 1613517770901.0,
"__v" : 0
}
Here my query function
Promise.all([
UserModel.findOne({ uuid: selfUuid }).lean(),
UserModel.findOne({ uuid: chatWithUuid }).lean(),
Chat.find({
$or: [
{ sendBy: uuid, sendTo: chatWithUuid },
{ sendBy: chatWithUuid, sendTo: uuid },
],
})
.lean()
.sort({ createDateTimestamp: -1 })
]).then((result) => {
const selfUser = result[0]
const chatWithUser = result[1]
const chatMessages = result[2]
const responseData = chatMessages.map(({ sendBy, sendTo, ...props }) => {
if (sendTo === selfUser.uuid) {
return {
...props,
sendTo: selfUser,
sendBy: chatWithUser,
}
} else {
return {
...props,
sendTo: chatWithUser,
sendBy: selfUser,
}
}
})
response.success(res, responseData)
}).catch((e) => {
response.fail(res, e, 'fail to getSingleChat')
})
Upvotes: 0
Views: 260
Reputation: 161
If you give index to collection for createDateTimestamp in mongodb. You don't need .sort({ createDateTimestamp: -1 }) anymore.
Upvotes: 1
Reputation: 10737
Based on your query function and model you can create following two indexes to speed up your searches:
1) {uuid:1}
2) {sendBy:1,sendTo:1}
Upvotes: 1