Max Zemsky
Max Zemsky

Reputation: 322

Mongoose: return only values

I am using Mongoose to query my User schema, which contains:

var usersSchema = new Schema( 
    {
        email : String,
        regId : { type : String , default : '' },
        lastName : { type : String , default : '' },
        ...
    });

I need to get getId property values for the array of emails i have. Currently i am trying to do:

db.model('users').find( { email : { $in : arrUsers } }, { regId : true, _id : false }, function (err, res) { ... }

I receive this array: [{ regId : "..." }, { regId : "..." }] .

What i want to receive: { "...", "..." } .

Is there an easy way to do this? Thanks a lot !!!

EDIT: I am interested in doing the for loop on the database side...

Upvotes: 1

Views: 4099

Answers (1)

Anton Savchenko
Anton Savchenko

Reputation: 1220

You can use additional underscores map function:

var regIds = _.map(res, function (element) {
  return element.regId;
});

//regIds = ['..', '..'] as you've expected

Upvotes: 4

Related Questions