Mongoose InsertMany不返回结果中保存文档的_id

时间:2018-02-15 10:27:41

标签: mongodb mongoose mongoose-schema

我试图用猫鼬将车辆列表保存到我的mlab(mongodb)中。这是我的疑问:

Vehicle.insertMany(req.body, function(err, result){
        //**req.body is a list of Vehicle objects**
        if (err){
            res.status(400).send( { message: 'Could not save the vehicles', meta: err } );
        } 
        else{
            res.status(200).send( { message: 'Successfully saved the vehicles', data: result } );
        }
    });

这是我的车辆架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const vehicleSchema = new Schema({
    _id: String,
    name: String,
    type: String
});

const Vehicle = mongoose.model('vehicle', vehicleSchema);

module.exports = Vehicle;

Vehicle.InsertMany的结果返回以下内容:

{
    "message": "Successfully saved the vehicles",
    "data": [
          {
             "name": "KIA",
             "type": "four wheeler",
             "__v": 0
          },
          {
             "name": "BDY",
             "type": "four wheeler",
             "__v": 0
          }
     ],
}

我的问题是,为什么结果不会返回已保存文档的_id字段?什么是__v字段?

(PS:Mongoose docs在insertMany文档中没有提及_id的任何内容。另外,我是Mongoose的新手,不确定我的架构是否正确)

请指出正确的方向。感谢。

0 个答案:

没有答案