Is there a way to user mongoose.find({title:'some title'})
to query the same field with multiple values? For example something like this mongoose.find({title:'some title', title:'some other title'})
sends back only documents matching title:'some other title
is there a way to accomplish this ?
Mongoose lets you structure queries using chaining or, equivalently, using POJOs in a single function call. Model.
The limit method will be used to return the maximum number of results from the collection, while the skip method is used to skip the number of documents from the collection in MongoDB. If we have one collection name as a student, student collection contains a hundred documents in it.
Return value findById returns the document where the _id field matches the specified id . If the document is not found, the function returns null .
Mongoose async operations, like . save() and queries, return Promises/A+ conformant promises. This means that you can do things like MyModel.
You should use the MongoDB $in
operator -
mongoose.find({title: {$in: ['some title', 'some other title']}})
You provide an array to $in
operator and it will return all the documents which have an exact title in the array specified.
Found this in 2021 with the same question. It seems the $in operator is no longer necessary. As long as you pass find() an array of items, it will search for all of them.
Ex:
Furniture.find({
room: [ 'first_id', 'second_id' ]
})
This will return the documents where room is the first OR second id.
Can be written in a query string like:
?room=first_id&room=second_id
There's no parsing needed—Node parses this into an array automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With