mongoose的population能否实现多级的联表查询
发布于 9 年前 作者 kingche 7381 次浏览 最后一次编辑是 8 年前 来自 问答

case:

// mongoose.model('User', UserSchema);		
var mongoose = require('mongoose'),
  Schema = mongoose.Schema;
  var UserSchema = new Schema({
  name: {
  type: String,
  unique: 'Username already exists',
  required: 'Please fill in a username',
  trim: true   },
  group:  {
  type: Schema.Types.ObjectId ,
  ref: 'Group'
  }
  ……
  });
  // mongoose.model('Group', UserSchema);		
  var GroupSchema = new Schema({
  name: {
  type: String,
  unique: 'Groupname already exists',
  required: 'Please fill in a Groupname',
  trim: true   },
  collection:  {
  type: Schema.Types.ObjectId ,
  ref: 'Collection'
  }
  ……
  });
    // mongoose.model('Collection', UserSchema);		
var CollectionSchema = new Schema({
  title: {
  type: String,
  trim: true   }
  ……
  });
Q:User.findOne().poplation('group').exec(function(..){
可以获得群组的信息,但怎样通过这样的查询直接获得collection的title信息等?
})
3 回复

我也想知道

找到了,最新版的mongoose支持了deep-populate 官方文档: deep-populate

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/ mingodb 支持 aggregate lookup 可以关联查询两个表,多级查询,多个lookup

来自酷炫的 CNodeMD

回到顶部