簡體   English   中英

mongodb文檔中的附加ID字段

[英]additonal id field in mongodb document

我正在處理一個應用程序。 問題是當我使用郵遞員查看存儲在mongodb中的作物文檔時,也會出現一個額外的id字段,該字段與mongodb的_id字段相同。 這是id ,我知道mongodb提供的常規_id字段。

我有一個裁剪模式如下

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 },
});

郵遞員退回的實際文件是

{
"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"
}

我的方法如下所示:

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

您可以在要使用的字段中使用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