I have a collection "users" in my Database. A user can be a member of several teams.
I am using an Array to reference the team IDs.
{ _id: ObjectId("552544fd600135861d9e47d5)",
name : "User1",
teams: [
"5527a9493ebbe2452666c238",
"5527b1be3371e3a827fa602c"
]
}
The teams are nothing more than a collection of:
{ _id: ObjectId("5527a9493ebbe2452666c238"),
name: "Team 1"
}
{ _id: ObjectId("5527b1be3371e3a827fa602c"),
name: "Team 2"
}
Now I want to get the names of all the teams in which the user is a member.
I have only found the way of querying it this way:
db.teams.find(
{_id:{$in:
[ObjectId("5527a9493ebbe2452666c238"),
ObjectId("5527b1be3371e3a827fa602c")]
}})
To do this, I would need to create an array specifically for this query. I would rather try to avoid this, because I already have the IDs available as an array in the string format. Something like this would be great:
db.teams.find(
{_id:{$in:
["5527a9493ebbe2452666c238",
"5527b1be3371e3a827fa602c"] // Strings here, not ObjectIDs
}})
But this does not work. Is there any comfortable way of querying an ObjectID with a set of string IDs?
Thanks & regards Rolf
teamscollection you simply checkteamsids presents inuserscollection