在集合/对象中查找-SailsJS和Waterline ORM [英] Find in collection/object - SailsJS and Waterline ORM
问题描述
我使用的是SailsJS,所以使用Waterline ORM和MongoDB.
I'm using SailsJS, so Waterline ORM and MongoDB.
我在一个名为Labels的集合中的一个对象中保存了多个用户ID(这样,一个标签可以属于多个用户).
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).
我在Mongo中有一个数据结构,如下所示:
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)
}
}
如果我当前是ID为 101 的用户-我想查找()在labels.belongs_to中存在101的所有Label条目.
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})
这是解决此问题的最佳方法吗?如果是,我如何通过Sails实现这一目标?
Is this the best way to tackle this, and if so, how can I achieve this via Sails?
推荐答案
当前Waterline不支持查询嵌入式记录,主要是因为跨多个DB这样做不容易.
Currently Waterline does not support querying embedded records, mostly because this is not simple to do across multiple DBs.
但是,您可以使用Label.native(function(err, rawMongoCollection){/*...*/});
来获取原始的Mongo集合,并且应该可以执行类似的查询:
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.
这篇关于在集合/对象中查找-SailsJS和Waterline ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!