I am writing code with node.js and mongoose module. My used module is "find" method but mongo is getting all data to me so filter is not working. What is problem ?
Example Codes:
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/nodeV1').then(res=>console.log("Connection Success")).catch(e=>console.log("Error"))
const dbSchema = mongoose.Schema({},{collection:'users'})
const dbModel = mongoose.model('users',dbSchema)
dbModel.find({age:'28'},{age:1},(err,res)=>{
console.log(res)
})
Result:
{ _id: new ObjectId("62eaad6af17720bd31d4daab"), age: 27 },
{ _id: new ObjectId("62eaad6af17720bd31d4daac"), age: 40 },
{ _id: new ObjectId("62eaad6af17720bd31d4daad"), age: 24 },
{ _id: new ObjectId("62eaad6af17720bd31d4daae"), age: 28 },
{ _id: new ObjectId("62eaad6af17720bd31d4daaf"), age: 20 },
{ _id: new ObjectId("62eaad6af17720bd31d4dab0"), age: 26 },
{ _id: new ObjectId("62eaad6af17720bd31d4dab1"), age: 32 },
You’r seeing data is all data arrived
What is problem ?
3
Answers
You need to use the
$in
operator, and put the values in an array, like:simply update your code to this
I know it might late but if you are using latest mongoose version.
Please read migration to 6 guide
Plus You are searching by
string
onnumber
field. Ignoring fact it might solve problem.Problem explained:
which emulates to
when you have non-existing key in query you can try one of these solutions