猫鼬:return 只有值

Mongoose: return only values

我正在使用 Mongoose 查询我的用户架构,其中包含:

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

我需要为我拥有的电子邮件数组获取 getId 属性 值。目前我正在尝试做:

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

我收到这个数组:[{ regId : "..." }, { regId : "..." }] .

我想收到什么:{ "...", "..." } .

有没有简单的方法来做到这一点?非常感谢!!!

编辑:我有兴趣在数据库端执行 for 循环...

您可以使用额外的 underscores 地图函数:

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

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