Artemka
Artemka

Reputation: 53

How to read all documents from collection in mongoose?

Seems to be a simple question, but I can't find the answer. There is a collection of posts, documents of which have a field rubric. How to read ALL documents from this collection in mongoose using Post.find ({rubric: value}, function), but keeping rubric. What value has to be for this to work?

Upvotes: 1

Views: 594

Answers (1)

J.F.
J.F.

Reputation: 15177

To find according if a field exists whatever value it has, you can use $exists in this way:

db.collection.find({
  "rubric": {
    "$exists": true
  }
})

Using mongoose is the same query. Something like this:

yourModel.find({
  "rubric": {
    "$exists": true
  }
})

Example here.

Also, to know about these world in mongo world you can check documentation every time you have an issue.

At the left of the page you see "Reference > Operators" and there are a lof of different operators you can use.

Upvotes: 1

Related Questions