I have a Schema as follows:
Schema = mongoose.Schema
User= new Schema
{ name: String,
phones: [
{
confirmed: {
type: Boolean,
default: false
},
number: {
type: String,
unique: true
}
]}
I need to make a findOne query that looks for a certain name or a phone number of a but only if it is confirmed I tried the following but it is treating it as an or instead
const userFound = await User.findOne({
$or: [
{ name },
{ $and: [{ 'phones.number': phone }, { 'phones.confirmed': true }] }
]
});
Lets say i have the following document:
{"name": "foo", {"number": 1234, "confirmed": false}}
userFound is returning that document when my query parameters are name=foo2 , number=1234
Any ideas what i might be doing wrong?
Try with $elemMatch allows you to match more than one component within the same array element.
phones: {$elemMatch: {number:1234, confirmed: true}}