You don't need the $in to find something inside an array. A simple find will do the trick:
db.users.find({subscriptions : '52fd1a033004de2d9bcb4980'})
All users with that channel will be returned.
Question
I'm creating a simple application with Sail.js, and its Waterline ORM. For part of this, I'm following NoSQL's equivalent of a pivot table pattern: each user has an array, subscriptions
, of all channels they're subscribed to. I'm attempting to find all users subscribed to a particular channel id, using the following:
User.find({subscriptions: {$in: [req.param('channel')]}}).done(function (err, users) {
/* ... */
});
Not the most beautiful line of code every written, but it should work. The issue is, it doesn't. users
is an empty array, and err
is null. They query actual executed is, for example (using the Node Inspector):
{ subscriptions: { '$in': [ '52fd1a033004de2d9bcb4980' ] } }
When this same query is used to "find" users via the umongo GUI, it works correctly. However, no users are found when the function is executed.
And ideas? Thanks!
Solution 2
You don't need the $in to find something inside an array. A simple find will do the trick:
db.users.find({subscriptions : '52fd1a033004de2d9bcb4980'})
All users with that channel will be returned.
OTHER TIPS
Just FYI, the accepted answer is a valid Mongo query but not valid Sails code. You want:
User.find({subscriptions: req.param('channel')}).done(function (err, users) {
/* ... */
});
The reason your original code didn't work is that the Sails ORM (Waterline) doesn't understand native Mongo operators like $in
. However, since it uses the underlying Mongo adapter to perform the find
operation, a search for a single channel will match any records that include that channel in their subscriptions
array. Note that if you ever do need to use low-level Mongo operators (like $all
) you can use the native
method of your model class to get access to the underlying Mongo adapter. See this answer for more details.