How to populate array of objects (some fields of the objects or the whole object)

894 views
Skip to first unread message

valentin genev

unread,
Sep 9, 2014, 10:08:25 AM9/9/14
to mongoo...@googlegroups.com
Hi all, 

I have the following schemas: 

var documentsSchema = new Schema({

    "_project"          : {
        type : Schema.ObjectId, 
        ref  : 'Projects'
    },
    "_addedBy"          : {
        type : Schema.ObjectId, 
        ref  : 'Users'
    },
    "_associateUsers"   : [{
        type : Schema.ObjectId, 
        ref  : 'Users'
    }],
    "_codes"            : [{
        type : Schema.ObjectId, 
        ref  : 'Codes'
    }],
    "paragraphTitle"    : String,
    "paragraphText"     : String,

    "memos"             : [
    {
        "_addedBy"      : {
            type: Schema.Types.ObjectId, 
            ref: 'Users'
        },
        "memoData"      : String
    }
]});

and the codes: 

var codesSchema = new Schema({
"code" : {

    "_addedBy"      : {
        type    : Schema.ObjectId, 
        ref     : 'Users'
    },
    "codeText"      : String,
    "codeWeight"    : Number
}});

I need to populate _codes.codeText (or codes fields) of the all elements of the array, but looks like I am not doing it properly.

Documents.find({
            "document._project": element._id
            }).
            populate('document._codes.code','code.codeText'). 
            exec(function (err, result) { .... }

this and various tries with populate arguments are either not populating the fields or not returning any data.

What am I doing wrong?

My mongoose version is 3.8.16

valentin genev

unread,
Sep 9, 2014, 10:11:02 AM9/9/14
to mongoo...@googlegroups.com
actually the populate line is: 
populate('_codes.code','code.codeText'). 
Reply all
Reply to author
Forward
0 new messages