Y
Y
Yuri Gubich2014-06-08 15:22:42
MongoDB
Yuri Gubich, 2014-06-08 15:22:42

How to search nested documents in Mongoose?

Hello.
I am learning to use nodejs, mongoDB, mongoose, for the time being - solely in order to broaden my horizons.
Decided to try nested documents and ran into a problem.
There is a user object, which has a lot of static fields, methods, and a data field, which is an array of nested documents. In the data array, it is planned to store all information about user actions in general - page views, links to downloaded files, comments, messages, etc. In general, the data field will consist of thousands, tens of thousands of documents for each user document.
The question arises: how to select the necessary documents from the data field for a specific user (for example, all documents that have a 'type': 'message' field)?
From the absence in the mongoose manuals of a normal description of searching through nested documents (with the exception of searching by id), I got the impression that nested documents were not made for this use at all. Tell me, the method proposed above is really inefficient?
And in case this is inefficient, I will need to create a collection for each user containing the current data object. Can you tell me how to do this using mongoose?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Haunted, 2014-06-26
@Arhangel64

For nested documents, dot notation works, db.base.find({ date.type : "someType" }).
To search through nested arrays, there is the $elemMatch operator ( docs.mongodb.org/manual/reference/operator/query/e...
And of course, all this can be nested and combined to get the desired result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question