Javascript 猫鼬:你怎么不受欢迎?

Javascript 猫鼬:你怎么不受欢迎?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我正在使用,但有一种情况我不想填充。我怎么能不受欢迎 var userSchema = new Schema({ local: { type: ObjectId, ref: 'Local', autopopulate: true }, facebook: { type: ObjectId, ref: 'Facebook', autopopulate: true }, twitter: { type: ObjectId, ref: 'Twitter', autopopulate: tr

我正在使用,但有一种情况我不想填充。我怎么能不受欢迎

var userSchema = new Schema({
  local: { type: ObjectId, ref: 'Local', autopopulate: true },
  facebook: { type: ObjectId, ref: 'Facebook', autopopulate: true },
  twitter: { type: ObjectId, ref: 'Twitter', autopopulate: true },
  google: { type: ObjectId, ref: 'Google', autopopulate: true }
});

User
  .findById(req.params.id)
  .unpopulate(local)
  .exec()...

据我所知,mongoose autopopulate在执行之前使用预钩子配置find和findOne查询的填充选项。这类似于在查询中隐式附加populate选项

对于您的模式,使用mongoose autopopulate
User.findById(req.params.id)
将转换为

User.findById(req.params.id).populate('local')
.populate('facebook').populate('twitter').populate('google');
在mongoose autopopulate中无法删除它,除非在查询之前显式禁用所需字段的autopopulate选项


populate函数只是对数据库的另一个调用,用于获取引用的文档并将其嵌入到主文档中。因此,尽量避免在一开始就填充它,并且在已经获取它之后不要删除它。

据我所知,mongoose autopopulate在执行之前使用pre-hook在find和findOne查询上配置填充选项。这类似于在查询中隐式附加populate选项

对于您的模式,使用mongoose autopopulate
User.findById(req.params.id)
将转换为

User.findById(req.params.id).populate('local')
.populate('facebook').populate('twitter').populate('google');
在mongoose autopopulate中无法删除它,除非在查询之前显式禁用所需字段的autopopulate选项

populate函数只是对数据库的另一个调用,用于获取引用的文档并将其嵌入到主文档中。因此,尽量避免在一开始就填充它,并且在已经获取它之后不要删除它。

请参阅

看起来你是对的-看起来你是对的-