从参考subDocument属性数组中找到猫鼬(findOne)

时间:2020-06-02 18:17:28

标签: javascript mongodb mongoose

因此,我正在尝试使用poulate和match属性,但没有成功,但找不到任何有关我的问题的文档(我也很努力地找到有关该文档的文档,因此也许您可以提供一些参考资料而不是this)?)

顺便说一句,我有两个模型:

1-页面

const PageSchema = new Schema({
    name: String,
    organization: {
        type: Schema.Types.ObjectId,
        ref: 'organizations',
    }, 
})

1-组织

const OrganizationSchema = new Schema({
    name: String,
    domains: [{
        type: String,
        unique: true
    }],
})

我需要找到属于特定域的页面,因此我正在执行以下findOne查询:

const page = await  Page.findOne({ 
    'settings.slug':  req.params.slug
}).populate({
    path: 'organization',
    match: {domains:"foo.com"},
}).exec()

它不进行“过滤”,并返回具有无域“ foo.com”的组织的页面。

我尝试了一种更简单的方法,但是它也不起作用:

const presentation = await  Page.findOne({ 
    'organization.domains': host,
    'settings.slug':  req.params.slug
}).populate('organization')

它始终返回null。

有人可以帮助我吗?非常感谢

1 个答案:

答案 0 :(得分:0)

我认为问题出在这行

.populate({
    path: 'organization',  <---  you forgot an s //organizations as declared in you ref
相关问题