简体   繁体   English

mongodb文档中的附加ID字段

[英]additonal id field in mongodb document

I am working on an application. 我正在处理一个应用程序。 The problem is when i use postman to view the crop document stored in mongodb, an extra id field appears too which is same as the _id field of mongodb. 问题是当我使用邮递员查看存储在mongodb中的作物文档时,也会出现一个额外的id字段,该字段与mongodb的_id字段相同。 This is id , I am aware of the normal _id field that mongodb supplies. 这是id ,我知道mongodb提供的常规_id字段。

I have a crop schema as follows 我有一个裁剪模式如下

const mongoose = require('mongoose');
const Pesticide = require('./Pesticide');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;
const cropSchema = new mongoose.Schema({
nameOfCrop:{
  type:String,
  lowercase:true,
  required:'Please enter the name of the crop',
  trim:true
},
imageOfCrop:String,
soilType: String,
waterNeeded:String,
tagCrop:String,//forage, vegetable, oilseeds etc.
pesticideForCrop:[{
    type:mongoose.Schema.Types.ObjectId,
    ref:'Pesticide'
  }
]},
 {
toJSON: { virtuals: true },
toObject: { virtuals: true },
});

The actual document that gets returned by postman is 邮递员退回的实际文件是

{
"pesticideForCrop": [],
    "_id": "5af1d1d54558fae1d0010bb4",
    "nameOfCrop": "Tomato",
    "imageOfCrop": "tomatoimage",
    "soilType": " almost all soil types except heavy clay",
    "waterNeeded": "water once every two or three days",
    "tagCrop": "Vegetables",
    "id": "5af1d1d54558fae1d0010bb4"
}

My method looks like below: 我的方法如下所示:

exports.getCrops = function(req,res){
 Crop.find({}).populate('pesticideForCrop').exec(function (err, crops) {
 if (err) return err;
 res.send(crops);
 console.log(crops);
 });
 };

you can use select in populate for the fields which ever you want 您可以在要使用的字段中使用select in populate

Crop.find({}).populate({
    path: 'pesticideForCrop',
    select: {
        "_id": 1,
        "nameOfCrop": 1, 
        "imageOfCrop":1, 
        "soilType": 1, 
        "waterNeeded": 1,
        "tagCrop": 1
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM