Reputation: 1
I just started to learn mongoose. I used the find()
method to fetch all the tour data from the database.
When I don't pass anything to the find()
method, it shows all of the results, which is correct. However, when I try to filter all the documents and I pass the wrong object into find()
method, that also shows all the tour results.
my TourSchema
const { Schema, model } = require("mongoose");
const tourSchema=new Schema({
name:{
type:String,
trim:true,
unique:true
},
price:{
type:Number,
require:[true,"A tour must have price"],
},
ratingsAverage:{
type:Number
},
ratingQuantity:{
type:Number,
},
duration:{
type:Number
},
ratingsAverage:{
type:Number,
default:4,
min:1,
max:5
},
difficulty:{
type:String,
enum:["easy","medium","difficult"]
},
maxGroupSize:{
type:Number,
require:[true,"A tour must have group size"],
},
description:{
type:String,
trim:true,
require:[true,'A tour must have description']
},
imageCover:{
type:String,
require:[true,'A tour must have imageCover']
},
images:[String],
startDates:{
type:[Date]
},
summary:{
type:String,
trim:true,
require:[true,"A tour must have summary"]
}
})
const Tour=new model('Tour',tourSchema);
module.exports=Tour;
findAllTour.js
exports.getAllTour =async(req, res) => {
try{
// console.log(req.query)
const tours=await Tour.find({"sjfa":5});
res.status(200).json({
status: "success",
results: tours.length,
data: {
tours
},
})
}
I pass wrong field in find method, its work fine, means show all the tour
Upvotes: 0
Views: 1014
Reputation: 13289
This is the expected behavior since mongoose 6.0.10
which makes strictQuery
equal to strict
by default, filtering out properties that are not in the schema.
If you need to disable strictQuery
you can either set:
mongoose.set('strictQuery', false);
Or run:
const tours = await Tour.find({ "sjfa":5 }, null, { strictQuery: false });
For more information, check the official docs or this discussion.
Upvotes: 3