Find in collection/object - SailsJS and Waterline ORM

1.3k views Asked by At

I'm using SailsJS, so Waterline ORM and MongoDB.

I'm saving multiple user IDs in an object within a collection called Labels (this is so that a label can belong to 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'm currently user of id 101 - I want to find() all Label entries where 101 exists in labels.belongs_to.

I've searched the docs but can't 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 tackle this, and if so, how can I achieve this via Sails?

1

There are 1 answers

2
Dário On

Currently Waterline does not support querying embedded records, mostly because this is not simple to do across multiple DBs.

You can however use Label.native(function(err, rawMongoCollection){/*...*/}); to get a raw Mongo collection and with it you should be able to do a similar query: .native() docs.