Mongo query take a long time. How make it more fast?

后端 未结 2 1811
梦如初夏
梦如初夏 2021-01-14 07:39

I use mongoose driver in node js. My schema:

let sendResultSchema = mongoose.Schema({
  emailId: String,      email: String,              
  letterId: String         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-14 07:44

    If you don't need all the fields in the record you can make the query faster by returning just the necessary fields. Like;

    db.sendresults.find({"tag" : "tagValue", "letterId" : "5ad630b5949bb02ea07d15d1"},{"_id":0,"tag":1,"email":1}).sort({emailId: -1}).limit(1)
    

    exp. Making field equal to one means returning that field. I made "_id : 0" because I didn't want to get id of the record in mongodb. If you don't add "_id:0" to query it is gonna return the id automatically.

    While working with a record with more and nested fields, it saved me a lot of time.

提交回复
热议问题