FindOperator
Find All Records
const courses = await Course.find()
Where Condition
const courses = await Course.find({author:"Peter"}) //where author is Peter
Limit records
const courses = await Course.find().limit(10) //limit to first 10 records
Sort by
const courses = await Course.find().sort({name:1}) //1 ascending,-1 descending
Select fields
const courses = await Course.find().select({name:1,tags:1}) //only retrieve certain columns
Comparison Operators
//eq = Equal
//ne = Not equal
//gt = Greater than
//lt = Less
//gte = Greater than or equal to
//lte = Less than or equal to
//in = In
//nin = Not in
const courses = await.Course.find(price:10); //Get courses that is 10 dollars
const courses = await Course.find(price:{$gte:10,$lte:20}); //Get courses between 10 and 20 dollars
const courses = await Course.find(price:{$in:[10,15,20]}); //Get courses that is 10,15,20
Logical Operators
//or = or
//and = And
const courses = await Course.find().or([{author:"Peter"},{isPublished:true}]); //Get courses that has authored by Peter OR isPublished
const courses = await Course.find().and([{author:"Peter"},{isPublished:true}]); //Get courses that has authored by Peter AND isPublished
Regular Expressions
//^P/ starts with P
const courses = await Course.find({author:/^Pe/}); //Get courses that starts with Pe
const courses = await Course.find({author:/Pe$/}); //Get courses that ends with Pe
const courses = await Course.find({author:/.*Pe.*/}); //Get courses that contains Pe in the middle name
Pagination
const pageNumber = 2;
const pageSize = 10;
const courses = await Course.find().limit(pageSize).skip(pageNumber-1)*pageSize;
Count documents
const coursesCount = await Course.find().count();