S
S
Sarkis Arutiunian2016-08-25 23:02:45
JavaScript
Sarkis Arutiunian, 2016-08-25 23:02:45

Mongoose how to search inside an object nested in an array?

Let's say we have a collection consisting of the following type of objects:

Users = {
    firstName: 'String',
    lastName: 'String,
    otherInfo: [
        {_id: ''},
        {_id: ''},
        {_id: ''}
   ]
}

Is it possible to return objects by the '_id' key or just return all objects and then iterate over them? which I would not like to do for obvious reasons.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
napa3um, 2016-08-26
@sarkis-tlt

stackoverflow.com/questions/10885044/mongodb-how-t...

A
Anton Ptitsyn, 2016-08-26
@ciclodolum

You can bind the query via .populate:
model:

User = new Schema({
    firstName: 'String',
    lastName: 'String,
    otherInfo: [{type: Schema.ObjectId, ref: 'OtherInfoSchema'} ]
});

запрос:
запрос вернется с развернутой otherInfo:
... 
  lastName: 'String,
    otherInfo: [
        {_id: '0', name: '', ... },
        {_id: '1', name: '', ... }
   ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question