U
U
uRoot2022-01-15 23:01:52
MongoDB
uRoot, 2022-01-15 23:01:52

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": "статьи"});


But the following result is returned:
61e327dc42d90853749672.png
And I would like the collections with the search word to be returned completely. How can I do this using mongoose?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nowm, 2022-01-16
@uroot

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, titleand from the results text.
Methodselectin 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.

M
Maxim Kirshin, 2022-01-16
@meowto16

https://docs.mongodb.com/manual/reference/method/d...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question