How to get array of json objects rather than mongoose documents

前端 未结 2 1003
暖寄归人
暖寄归人 2021-02-01 17:46

When I do find operation like the follows:

Collection.find({name: \'Erik\'}, function (err, docs) {
   // do momething
});

\'docs\' variable is

相关标签:
2条回答
  • 2021-02-01 18:32
    .exec(function(err, docs){
        docs= docs.map(o => o.toObject());
    

    This will include virtuals and getters

    0 讨论(0)
  • 2021-02-01 18:51

    If you're using Mongoose 3.x you can use the lean query option to do this:

    Collection.find({name: 'Erik'}).lean().exec(function (err, docs) {
        // docs are plain javascript objects instead of model instances
    });
    
    0 讨论(0)
提交回复
热议问题