Answer the question
In order to leave comments, you need to log in
How to find and return records in Mongodb?
How to search a collection in Mongodb for some fields, and return the results found?
I try like this:
await Post.find({}).select({ "title": "статьи","text": "статьи"});
Answer the question
In order to leave comments, you need to log in
This should be the parameter of the find method: { "title": "статьи","text": "статьи"}
. But you have an empty object as a criterion for the query (this means that all documents from the collection will be selected), and an additional object is specified in select, which says that you need to take the fields _id
, title
and from the results text
.
Methodselect
in Mongoose, by the way, it is used a little differently. When you pass an object as a parameter to it, you need to specify either 1 (the field is included in the result) or 0 (the field is not included in the result) values. If you pass a non-empty string as a value, it will be treated as 1, but this will distort your understanding of what is happening. Select is only an indication of which fields should be in the result, this method does not affect the condition for selecting documents from the collection at all. The selection conditions are specified in find.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question