Using $in in MongooseJS with nested objects


I've been successfully using $in in my node webservice when my mongo arrays only held ids. Here is sample data.

{ "_id": { "$oid": "52b1a60ce4b0f819260bc6e5" }, "title": "Sample", "team": [ { "$oid": "52995b263e20c94167000001" }, { "$oid": "529bfa36c81735b802000001" } ], "tasks": [ { "task": { "$oid": "52af197ae4b07526a3ee6017" }, "status": 0 }, { "task": { "$oid": "52af197ae4b07526a3ee6017" }, "status": 1 } ] }

Notice that tasks is an array, but the id is nested in "task", while in teams it is on the top level. Here is where my question is.

In my Node route, this is how I typically deal with calling a array of IDs in my project, this works fine in the team example, but obviously not for my task example.

app.get('/api/tasks/project/:id', function (req, res) { var the_id = req.params.id; var query = req.params.query; models.Projects.findById(the_id, null, function (data) { models.Tasks.findAllByIds({ ids: data._doc.tasks, query: query }, function(items) { console.log(items); res.send(items); }); }); });

That communicates with my model which has a method called findAllByIds

module.exports = function (config, mongoose) { var _TasksSchema = new mongoose.Schema({}); var _Tasks = mongoose.model('tasks', _TasksSchema); /***************** * Public API *****************/ return { Tasks: _Tasks, findAllByIds: function(data, callback){ var query = data.query; _Tasks.find({_id: { $in: data.ids} }, query, function(err, doc){ callback(doc); }); } } }

In this call I have <strong>$in: data.ids</strong> which works in the simple array like the "teams" example above. Once I nest my object, as with "task" sample, this does not work anymore, and I am not sure how to specify $in to look at data.ids array, but use the "task" value.

I'd like to avoid having to iterate through the data to create an array with only id, and then repopulate the other values once the data is returned, unless that is the only option.


I had a thought of setting up my mongo document like this, although I'd still like to know how to do it the other way, in the event this isn't possible in the future.

"tasks": { "status0": [ { "$oid": "52995b263e20c94167000001" }, { "$oid": "529bfa36c81735b802000001" } ], "status1": [ { "$oid": "52995b263e20c94167000001" }, { "$oid": "529bfa36c81735b802000001" } ] }


You can call map on the tasks array to project it into a new array with just the ObjectId values:

models.Tasks.findAllByIds({ ids: data.tasks.map(function(value) { return value.task; }), query: query }, function(items) { ...


Have you try the $elemMatch option in find conditions ? <a href="http://docs.mongodb.org/manual/reference/operator/query/elemMatch/" rel="nofollow">http://docs.mongodb.org/manual/reference/operator/query/elemMatch/</a>


  • Modify a Google App Engine entity id?
  • How can I see a list of all files that are different between two Hg repositories?
  • Receiver has no segue with identifier“***”
  • iOS Localization Doesn't Work with More Than 63 Files
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • Does Apportable support to build library binary (.a/.so)?
  • Updating Dojo provide
  • Simple linked list-C
  • CakePHP ACL tutorial initDB function warnings
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • How to attach a node.js readable stream to a Sendgrid email?
  • Switching to Release Build causes runtime error in Web Reference
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • QLineEdit password safety
  • Jenkins: How To Build multiple projects from a TFS repository?
  • req.body is undefined - nodejs
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Can Jackson SerializationFeature be overridden per field or class?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Symfony2: How to get request parameter
  • Redux, normalised entities and lodash merge
  • Hazelcast - OperationTimeoutException
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • Why winpcap requires both .lib and .dll to run?
  • QuartzCore.framework for Mono Develop
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • RestKit - RKRequestDelegate does not exist
  • Arrays break string types in Julia
  • Is there a mandatory requirement to switch app.yaml?
  • Android Studio and gradle
  • Revoking OAuth Access Token Results in 404 Not Found
  • Hits per day in Google Big Query
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can i traverse a binary tree from right to left in java?