I need to query entries in an array, the document looks like this:
{
"_id":"5d7b4ef6f691b71b5097e9cb",
"name":"1568362230828",
"commands":[
{
"_id":"5d7b4ef6f691b71b5097e9d1",
"name":"Command - 0"
},
{
"_id":"5d7b4ef6f691b71b5097e9d0",
"name":"Command - 1"
},
{
"_id":"5d7b4ef6f691b71b5097e9cf",
"name":"Command - 2"
},
{
"_id":"5d7b4ef6f691b71b5097e9ce",
"name":"Command - 3"
},
{
"_id":"5d7b4ef6f691b71b5097e9cd",
"name":"Command - 4"
},
{
"_id":"5d7b4ef6f691b71b5097e9cc",
"name":"Command - 5"
}
],
"__v":0
}
now i want to get all commands by there id:
model.find({
commands: {
_id: ["5d7b4ef6f691b71b5097e9cf", "5d7b4ef6f691b71b5097e9cf"] }})
Pseudo query, this does not work!
How must my query looks like ?!
const schema = new mongoose.Schema({
name: String,
commands: [{
name: String
}]
});
const model = mongoose.model('Endpoints', schema);
model.find({"commands._id": {$in:["5d7b4ef6f691b71b5097e9cf", "5d7b4ef6f691b71b5097e9cf"] }})will return you all models that have at least one command with given id.