An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
March 19, 2022 07:21 am GMT
Original Link: https://dev.to/deepakjaiswal/make-express-server-fast-request-response-using-indexing-in-mongodb-49pm
Make Express Server fast Request & Response Using Indexing in mongoDB
Hey Developers Today we discuss on topic how we fast our server.
so we learn about indexing in express of mongoose.
normally we can make schemas like
const {Schema,model}=require('mongoose');const userSchema=new Scheam({ name:{ type:String, required:true }, email:{ type:String, required:true, unique:true }, isDeleted:{ type:Boolean, default:false },});const UserModel=model('User',userSchema);module.export=UserModel;
normally we can use to check user on their email. in mongoDB unique
field auto index but other field not index.
const user=await User.findOne({email: "[email protected]" , isDeleted:false});
In above query of mongoose it takes more time because isDeleted field not indexed. so we make isDeleted field as index. so refactor model code.
const {Schema,model}=require('mongoose');const userSchema=new Scheam({ name:{ type:String, required:true, index:true }, email:{ type:String, required:true, unique:true }, isDeleted:{ type:Boolean, default:false, index:true },});const UserModel=model('User',userSchema);module.export=UserModel;
we make name is also in index because in searching query we check from name thats why we make as indexed field.
after make as index field you can see in mongodb document indexed field.
Original Link: https://dev.to/deepakjaiswal/make-express-server-fast-request-response-using-indexing-in-mongodb-49pm
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To