I'm new to MongoDB and have been having a bit of difficulty setting up a particular query for a new project I'm working on.
I have a data structure that looks like this (simplified version):
games: {_id: ..., scenes: [{_id: ..., views: [{_id: ...}]}]}
(i.e. games contains a collection of scenes, scenes contains a collection of views).
What I want to query here is a particular view object. I suppose the answer involves using $elemMatch, but how do I set this up? After a bit of research + playing around, I know I can do this to get the scene:
db.collection("games").findOne({
_id: ObjectId(req.params.gid)},
{
scenes: {
$elemMatch: {_id: ObjectId(req.params.sid)}
}
}...
But how do I extend this so that it only pulls the particular view I'm interested in (by _id)?
I guess I could always find the view object I'm looking for manually using a for loop, which brings up another question. Wrt performance, is better to do queries like this using Mongo, or manually by pulling the entire document to loop through collections?
result.scenes[0].views[0]? I'm okay with the latter.