How to get content of objectId in an Array - Mongoose / NodeJs

I would like to get an array/object of my tags with the content of my objectId (found by user id).

userModel

var userSchema = new mongoose.Schema({
    pseudo : { type : String },
    fullname : { type : String },
    password : { type : String },
    email : { type : String },
    //tags : { type : Array },
    tags : [
        {
            objectId : { type : mongoose.Schema.Types.ObjectId, ref: 'tag'},
            completed : { type : Boolean }
        }
    ],
    dateCreation : { type : Date, default : Date.now }
});

tagModel

var tagSchema = new mongoose.Schema({
  name : { type : String },
  alias : { type : String },
  description : { type : String },
  dateCreation : { type : Date, default : Date.now }
});

mongoose request

userModel.find({ _id : req.session.passport.user._id }).select('tags').populate('objectId', 'name alias').lean().exec(function (err, result) {
    console.log(JSON.stringify(result));
});

console.log

[
    {
        "_id":"538efe4a3bb9d97018b90ee4",
        "tags":[
                   {
                    "objectId":"538f25f7f4d621281b7376e9", 
                    "completed":false, 
                    "_id":"538f25f7f4d621281b7376ea"
                   }
        ]
    }
]

I would like to get content of objectId :/ Like 'name', 'alias'...

I know that I can write two request to get content, but it's not optimized :/

Can you help me ?

Thanks,

Answers


You need to provide the full, dot-notation path of the field to populate in your populate call:

userModel.find({ _id : req.session.passport.user._id })
    .select('tags')
    .populate('tags.objectId', 'name alias')
    .lean()
    .exec(function (err, result) {
        console.log(JSON.stringify(result));
    }
);

Need Your Help

Division in vb.net

vb.net division

What's the difference between / and \ for division in vb.net? My code gives very different answers depending on which I use. I've seen both before, but never knew the difference.

CakePHP - Form->select doesn't have a wrapping div?

php forms cakephp select wrap

I'm wondering why $this->Form->select() in CakePHP 2.1 doesnt come with a wrapping div and label.