//controllers/tourController.js
const Tour = require('./../models/tourModel');
exports.getAllTours = async (req, res) => {
try {
const queryObj = { ...req.query };
const excludeFields = ['page', 'sort', 'limit', 'fields'];
excludeFields.forEach((el) => delete queryObj[el]);
//build a query
const query = Tour.find(queryObj);
//execute query
const tours = await query;
res.status(200).json({
status: 'sucess',
results: tours.length,
data: {
tours,
},
});
} catch (error) {
res.status(404).json({
status: 'fail',
message: error,
});
}
};
model.find({}) instanceof mongoose.Query //true
model.find({}).lean() instanceof mongoose.Query //true
1.
model.updateOne(
{ <array>: value ... },
{ <update operator>: { "<array>.$" : value } }
)
db.students.updateOne(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)