简体   繁体   中英

Mongoose, check if value exists in an array of objects

I have a collection with: I want to use the $in operator

Person = {
 name: String,
 members: [ {id: String, email: String}... {}]
}

I use this so far:

Person.find({members: {"$in": [id1]}}) 

But I already know the flaw: If the array of members was like Members: [id1, id2, ... id3] that method would work. But it is an array of objects. So how do I get around it?

$elemMatch can be used with Arrays of Embedded Documents :

In your case you could try:

Person.find({ 
   members: { 
      $elemMatch: { id: id1 } 
   }
}); 

But since it is a Single Query Condition :

Person.find({ 
   "members.id": id1
}); 

Would do the trick.

You can use elem match in the following way:

db.collection.find({
  arrayfield: {
    $elemMatch: {
      id: ObjectId("5eaaeedd00101108e1123461")
    }
  }
})

The same can be done in mongoose in the following ways:

query.elemMatch('arrayfield', { id: ObjectId("5eaaeedd00101108e1123461") })

.

query.where('arrayfield').elemMatch({ id: ObjectId("5eaaeedd00101108e1123461") })

.

query.elemMatch('arrayfield', function (elem) {
  elem.where('id').equals(ObjectId("5eaaeedd00101108e1123461"));
})

.

query.where('arrayfield').elemMatch(function (elem) {
  elem.where({ id: ObjectId("5eaaeedd00101108e1123461") });
})

I have used this example collection:

[
  {
    "_id": ObjectId("5eaaeedd00101108e1123451"),
    "arrayfield": [
      {
        id: ObjectId("5eaaeedd00101108e1123461"),
        name: "David"
      },
      {
        id: ObjectId("5eaaeedd00101108e1123462"),
        name: "Brown"
      }
    ]
  },
  {
    "_id": ObjectId("5eaaeedd00101108e1123452"),
    "arrayfield": [
      {
        id: ObjectId("5eaaeedd00101108e1123471"),
        name: "Maple"
      },
      {
        id: ObjectId("5eaaeedd00101108e1123472"),
        name: "Green"
      }
    ]
  },
  {
    "_id": ObjectId("5eaaeedd00101108e1123453"),
    "arrayfield": [
      {
        id: ObjectId("5eaaeedd00101108e1123461"),
        name: "David"
      },
      {
        id: ObjectId("5eaaeedd00101108e1123482"),
        name: "Lacey"
      }
    ]
  }
]

Want to try live? Try it on mongo playground with this link https://mongoplayground.net/p/H3fdmp9HkQv

您可以在查询中使用members.id作为字段来匹配您的子文档id s :

Person.find({ "members.id": { "$in": ["id1"] } })

You can use the MongoDB $elemMatch operator to match documents where a nested array contains at least one element that matches a specified condition. In your case, you can use it to find the documents where the members array contains an object with the specified id. This is an example:

 Person.find({ "members": { "$elemMatch": { "id": id1 } } });

This will return all the documents where the members array contains an object with an id equal to id1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM