Reputation: 959
hope you doing well.
Guys I am looking for solution of mongoose problem, I have a object field (let say: vehicleStatus) in my User collection. Which has some keys (all are boolean values). Now I want to search from User collection based on this vehicleStatus field.
This is my Schema -
vehicleStatus: {
_id: false,
twoWheeler: Boolean,
fourWheeler: Boolean
},
Now I'm searching for user if they have twoWheeler, fourWheeler or both. For example, let say John doe has {twoWheeler: true, fourWheeler: false}. So what will be the best approach to search field like this (vahicleStatus) in mongoose.
Any kind of help will be appreciated.
Upvotes: 0
Views: 599
Reputation: 939
Now I'm searching for user if they have twoWheeler, fourWheeler or both.
User.find({
$expr: {
$or: [{
$eq: ['$vehicleStatus.twoWheeler', true]
}, {
$eq: ['$vehicleStatus.fourWheeler', true]
}]
}
}).then(console.log).catch(console.log);
You can check it here Mongo Playground
Upvotes: 1
Reputation: 9149
You can search for your matching value like this:
const users = await User.find({
'vehicleStatus.twoWheeler': true,
'vehicleStatus.fourWheeler': true,
});
Instead of true
, you can also put your variable.
Upvotes: 0