问题
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