Find in Collection / Object - SailsJS and Waterline ORM

I am using SailsJS, so Waterline ORM and MongoDB.

I am storing multiple user ids in an object in a collection called Labels (which means a label can be owned by multiple users).

I have a data structure in Mongo something like this:

labels: {
    id: ...
    belongs_to: {
        **id of user**: 2 (I'm using this is for individual user ordering)
    }
}

      

If I am currently user id 101 - I want to find () all label entries where 101 exists in label.belongs_to.

I have searched for docs but cannot find how to do this.

I've tried (with no luck) something similar to:

Label.find().where({ belongs_to: {'contains' : user_id})

      

Is this the best way to handle this, and if so how can I achieve this across the sails?

+1


source to share


1 answer


Waterline does not currently support querying inline records, mainly because it is not easy to do with multiple databases.



However, you can use Label.native(function(err, rawMongoCollection){/*...*/});

to obtain raw Mongo collection, and with it you will be able to perform a similar request: . native () docs .

+3


source







All Articles