匹配具有不同字段的mongo数组

时间:2019-07-10 12:04:17

标签: mongodb mongodb-query

这是我所拥有的示例订购文件

//Order 
{
    other fields,
    expertise = [
       { industry: "I1", service: "S1", competence: "C1", qualified: true },
       { industry: "I1", service: "S1", competence: "C2" }
    ]
}

这是用户文档的示例

//用户1

{
    other fields,
    expertise = [
        { industry: "I1", service: "S1", competence: "C1", qualified: true },
        { industry: "I1", service: "S1", competence: "C2" }
    ]
}

//User 2
{
    other fields,
    expertise = [
        { industry: "I1", service: "S1", competence: "C1", qualified: false },
        { industry: "I1", service: "S1", competence: "C2" }
    ]
}

qualified属性是布尔值和可选属性。

要求是获得给定订单的匹配用户(使用专业知识)。

订单中的所有专业知识应包括用户对象。 但是,

  1. 如果qualified中的trueexpertise,则需要返回Users,而qualifiedtrue

  2. 如果qualified中的falseexpertise,则需要返回Usersqualifiedtrue都是false

以下查询成功,否定1。

db.getCollection('users').find({
    expertise: {
        $all: [
           { industry: "I1", service: "S1", competence: "C1", qualified: true },
           { industry: "I1", service: "S1", competence: "C2" }
        ]
    }
});

但是要获得成功2,我需要查询以下内容。

db.getCollection('users').find({
    expertise: {
        $all: [
           { industry: "I1", service: "S1", competence: "C1", qualified: {$or: [true, false]} },
           { industry: "I1", service: "S1", competence: "C2" }
        ]
    }
});

这不起作用。如何将其修改为成功No2。任何帮助将不胜感激。

0 个答案:

没有答案