Javascript 在mongoose架构中使用ref填充集合失败
我想将信用集合填充到用户集合中,这是我的信用模式Javascript 在mongoose架构中使用ref填充集合失败,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我想将信用集合填充到用户集合中,这是我的信用模式 const CreditSchema = new Schema({ userId: Schema.Types.ObjectId, credit: { type: Number, default: 0 }, log: [String] }) 我的用户模式是这样的 const UserSchema = new Schema({ fullName: { type: String, trim: tru
const CreditSchema = new Schema({
userId: Schema.Types.ObjectId,
credit: {
type: Number,
default: 0
},
log: [String]
})
我的用户模式是这样的
const UserSchema = new Schema({
fullName: {
type: String,
trim: true,
required: true
},
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
required: true
},
password: {
type: String,
required: true
},
credit: {type: Schema.Types.ObjectId, ref: 'Credit' } //I suspect something is not right here
})
为什么我在下面的用户对象中看不到信用
const user = await User.findOne({_id: req.query.id})
.populate('credit')
.exec()
console.log(user) // credit field is not present?
如何保存文档?mongoDB中是否保存了信用字段?@GeorgeBailey在那里,信用的用户id与用户id匹配。@FarhanTahir是的,我有。