mongoose的population能否实现多级的联表查询
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