我希望有一个包含多个字段引用多个集合的集合,如下所示:
var comboSchema = new Schema({
oneId: { type: Schema.Types.ObjectId, ref: "One" },
twoId: { type: Schema.Types.ObjectId, ref: "Two" },
threeId: { type: Schema.Types.ObjectId, ref: "Three" },
components: {
id: {type: Schema.Types.ObjectId, ref: "Component"},
amount: {type: Number}
}
} 我知道我可以使用$lookup和aggregate来获取数据,但它看起来只适用于集合中的单个字段?
有什么帮助吗?谢谢!:-)
发布于 2019-03-06 19:24:28
这是一个使用ref的模型示例,对象中的ref键将采用您正在引用的模型的名称
const mongoose = require('mongoose');
const postSchema = mongoose.Schema({
text: {
type: String,
required: 1
},
mediatype: {
type: String,
required: 1
},
media: {
type: String,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
},
likes: {
type: [{
userid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
}
}]
},
comments: {
type: [{
userid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'user'
},
comment: String
}]
},
}, {
timestamps: true
})
const Post = mongoose.model('post', postSchema)
module.exports = Post这样您就可以像Post.find().populate('user')一样填充它
https://stackoverflow.com/questions/55021810
复制相似问题