0
我是新来的mongodb和练习aggregate
与$lookup
在猫鼬下面的文件,我遇到了一个问题,真的很混乱。
我有用户模式
const userSchema = new Schema({
userName: {type: String, index:{unique: true}},
password: String,
avatar: String
})
评论架构
const commentSchema = new Schema({
post: {type: Schema.Types.ObjectId, ref:'postModel'},
author:{type: Schema.Types.ObjectId, ref:'userModel'},
content: String,
createTime: Date
})
和模型commentModel= mongoose.model('commentModel', commentSchema)
userModel = mongoose.model('userModel', userSchema)
话,我
commentModel.aggregate.([{$lookup: {
from: 'userModel',
localField: 'author',
foreignField: '_id',
as: 'common'
}])
但我得到的查询为空常见。根据db中的数据,我不应该得到这个结果。搜索后,我仍然无法找到错误。
最后但并非最不重要,我需要使用aggregate
方法,所以我必须在参考文件上使用$lookup
,它的剂量有意义吗?
谢谢您的回答,我已经编辑我的问题,对于短,我没有提到的模型部分,但我确实创建了这些模型。 – rookiect
根据更新的问题,集合名称应该是'usermodels'。你可以通过直接连接到mongodb和'show collections'来验证它。 – sidgate
谢谢sidgate。你指出这个问题,我连接mongo shell并使用'usermodels'而不是'userModel',它可以工作!但在我的早期实践中,比如'使用引用填充',我使用'userModel',它也可以工作。无论如何,谢谢你的回答,它可以防止我疯狂:)祝你有美好的一天! – rookiect