Asa*_*lid 1 populate mongoose mongodb node.js mongoose-populate
以下是用户集合的架构:
const Mongoose = require('mongoose')
const Schema = Mongoose.Schema
const userSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String
},
supporterOf: [{
type: Schema.Types.ObjectId,
ref: 'individual',
required: false
}],
})
module.exports = Mongoose.model('user', userSchema);
Run Code Online (Sandbox Code Playgroud)
我想填充“supporterOf”,它是个人的集合(参考:个人)。'supporterOf' 包含一个 ObjectId 数组。我遇到了将特定 objectId 与该 ObjectId 数组匹配的问题。谁能建议我如何将特定的 ObjectId 与填充函数中的 ObjectIds 数组匹配?
You have a reference of 'individual' in supporterOf and you want to populate only relevant object from the array of individuals? If this is right case then do the following:
YourUserModel.findById(userId, function (err, user) {
console.log(user)
}).populate({
path: 'supporterOf',
match: {
yourObjectOfIndividualDocument: yourMatchingIdOfIndividual
}
})
.exec()
Run Code Online (Sandbox Code Playgroud)
Replace yourObjectOfIndividualDocument: yourMatchingIdOfIndividual by name: 'abcd'.
Here name is the field of Individual document not of User document.
| 归档时间: |
|
| 查看次数: |
12487 次 |
| 最近记录: |