猫鼬填充-选择字段

时间:2020-05-21 20:01:20

标签: node.js mongodb mongoose

我正在尝试使用ref:''将数据嵌入到父文档中,这可以正常工作,并且我能够检索数据,但是问题是我想排除一些我检查过文档的字段并发现select:'-field_name'可以做到,但对我不起作用。 下面是代码片段:

 // guides: Array // Used for embedding documents
        guides: [
            {
                type: mongoose.Schema.ObjectId,
                ref: 'User'
            }
        ] 

// Query Middleware 

    toursSchema.pre(/^find/, function(next) {
        this.populate({
           path: 'guides',
           select: '-__v' 
        });
        next();
    })

文档结构如下:

{
   "_id":{},
   "startLocation":{},
   "ratingsAverage":5,
   "ratingsQuantity":9,
   "images":[],
   "createdAt":{},
   "startDates":[],
   "secretTour":false,
   "name":"Mount Everest",
   "duration":5,
   "maxGroupSize":25,
   "difficulty":"easy",
   "price":3000,
   "summary":"Lorem ipsum...",
   "imageCover":"tour-1-cover.jpg",
   "locations":[],
   "slug":"Mount-everest",
   "__v":0
}

对此的任何帮助,我们将不胜感激。 :)

0 个答案:

没有答案