Can we do $all query in sails/waterline js?

亡梦爱人 提交于 2020-02-05 09:22:08

问题


I am using mongodb and sails/waterlinejs

One of my documents has a field "participants": ["1" ,"2" ,"3"]

I would like to make a mongodb query:

Document.find({participants: {$all: ["1", "2"]}})

But I notice that if I do it in waterline, it returns me all the documents that contains EITHER "1" OR "2".

I would like to get BOTH "1" AND "2"

Is this something that I have to do in native query? Does waterline have a doc somewhere that lists all the available operators for .find()?

Thanks


回答1:


Try to use $and, You should do like this.

query = Model.where({"$and": [ { "participants": "1" }, { "participants": "2" } ] });



回答2:


You could use the modifier in.

Searches for records where the value is in the list of values.

Model.find({
  participants: { in: ['foo', 'bar'] }
})

Docs: https://sailsjs.com/documentation/concepts/models-and-orm/query-language#?in

Edit

Provide an array to find records whose value for this attribute exactly matches any of the specified search terms.

Model.find({
  participants : ['foo', 'bar']
});

Docs: https://sailsjs.com/documentation/concepts/models-and-orm/query-language#?in-modifier



来源:https://stackoverflow.com/questions/53434879/can-we-do-all-query-in-sails-waterline-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!