mongodb populate 过后的查询问题
我有两个collection:
user:
let userSchema = mongoose.Schema({
userid: String,
name: String,
avatar: String,
mobile: String,
roles: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Role'
}]
}, {
versionKey: false
})
let User = mongoose.model('User', userSchema)
和role:
let roleSchema = mongoose.Schema({
name: String,
nike: String
}, {
versionKey: false
})
let Role = mongoose.model('Role', roleSchema)
现在有以下数据:
> db.user.find()
{
"_id" : ObjectId("5dc371291c7f84365fa05eba"),
"userid" : "suadmin",
"name" : "超级管理员",
"roles" : [
ObjectId("5dc6767a1c7f84365fa0a377")
]
},
{
"_id" : ObjectId("5dc681311c7f84365fa0a7c0"),
"userid" : "lisi",
"password" : "123456",
"avatar" : "",
"mobile" : "15652663001",
"name" : "李四",
"roles" : [
ObjectId("5dc679301c7f84365fa0a4f1")
]
}
> db.roles.find()
{
"_id" : ObjectId("5dc6767a1c7f84365fa0a377"),
"name" : "超级管理员",
"nike" : "suadmin"
},
{
"_id" : ObjectId("5dc679301c7f84365fa0a4f1"),
"name" : "普通用户",
"nike" : "normal"
},
{
"_id" : ObjectId("5dc79af38dd5e510e6ab0db7"),
"name" : "评审员",
"nike" : "checker"
}
然后我用populate role:
User.find().populate({
path: 'roles',
module: 'Role'
}).then(res => {
console.log(res)
})
===>
{
"_id" : ObjectId("5dc371291c7f84365fa05eba"),
"userid" : "suadmin",
"name" : "超级管理员",
"roles" : [
{
"_id" : ObjectId("5dc6767a1c7f84365fa0a377"),
"name" : "超级管理员",
"nike" : "suadmin"
}
]
},
{
"_id" : ObjectId("5dc681311c7f84365fa0a7c0"),
"userid" : "lisi",
"password" : "123456",
"avatar" : "",
"mobile" : "15652663001",
"name" : "李四",
"roles" : [
{
"_id" : ObjectId("5dc79af38dd5e510e6ab0db7"),
"name" : "评审员",
"nike" : "checker"
}
]
}
然后问题来了 我要怎么对populate后的数据过滤
比如我想找roles里有nike为checker的user
我这么查为什么没有结果:
User.find().populate({
path: 'roles',
module: 'Role'
}).where('roles.nike').equals('checker')
.then(res => {
console.log(res)
})
===>
[]
.where('roles.nike').equals('checker')
写得有问题吗
有没有大佬提点一下,困扰一晚上了
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在populate里加个
match