A
A
askold20132018-02-05 15:26:17
MongoDB
askold2013, 2018-02-05 15:26:17

How to make a selection on two fields of a mongodb subdocument?

Hello! I can't figure out why my sample doesn't work correctly.
I am writing this query:

documents
    .find(
      { "routes._id": req.session.userId, token: req.session.userGroup, "routes.canSee": 'yes' },
      { document: 0 }
    )

And I want to get documents in which the conditions routes._Id AND routes.canSee simultaneously match. That is, the routes subdocument with the given _id must have canSee: 'yes', otherwise the document must not be included in the selection.
But I get a selection in which the documents pass by the condition OR "routes._id": req.session.userId OR "routes.canSee": 'yes'
So I get the following array of documents:
[{
  ...какие то поля обьекта документа
 routes: [ { _id: '5a721c536ba3e60ae446d66c',
    role: 'Доцент',
    author: 'Шестопалов Сергей Викторович',
    canSee: 'yes',
    status: 'waiting' }]
 },
  { _id: '5a747c3d182e5a59e7f329c2', // по этому условию прошло
    role: 'Профессор',
    author: 'Кимкин Василий Викторович',
    canSee: 'no', // а по этому нет, но документ вошел в выборку
    status: 'waiting' } ]
}]

What is the problem? With the fields of a document (not a subdocument), this rolls - what's wrong with subdocuments?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question