Reputation: 38
I'd like to find data from mongodb using find() function Here is my schema
const newcontractSchema = mongoose.Schema({
creator: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: false,
index: true,
},
status: {type: String, required: false, default: 'init'},
a_status: {type: String, required: false, default: 'pending'},
contractInfo: {
startDate: {type: Date},
endDate: {type: Date},
expectedUsage: {type: Number, default: 2000},
provider: {type: Object},
rate: {type: Number},
term: {type: Number},
userid: {type: String}
}, {timestamps: true});
and what I tried is
Newcontract.find({contractInfo: {userid: {$eq: req.body.userid}}})
But I couldn't fetch data based on this userid in contractInfo object
How to do it?
Thanks
Upvotes: 1
Views: 8746
Reputation: 15187
You are not querying correctly.
You need something like this:
db.collection.find({
"contractInfo.userid": "yourid"
})
Mongo playground example here
In mongoose
will be very similar:
Newcontract.findOne({
"contractInfo.userid": req.body.id
})
Note that you can use findOne()
to get only one object if you want.
Upvotes: 6