猫鼬 - 查找文档,其中阵列没有任何比赛

问题描述 投票:1回答:1

下面是我收集的模式:

{
    _id: {
        type: String,
        required: true,
        unique: true
    },
    translations: [{
        language: String,
        translation: String
    }]
}

如果我有语言的数组,["spanish", "french"],我怎么能找到所有地方Collection.translations没有用于阵列中的语言中的至少一个对象的文件?

例如,这应选择:

{
    _id: "hello",
    translations: [{
        language: "not spanish",
        translation: "not hola"
    }]
}

但不是这些:

{
    _id: "hello",
    translations: [{
        language: "spanish",
        translation: "hola"
    }]
}
//and
{
    _id: "hello",
    translations: [{
        language: "spanish",
        translation: "hola"
    }, {
        language: "french",
        translation: "bonjour"
    }, {
        language: "not spanish",
        translation: "not hola"
    }]
}

这是我到目前为止有:

Model.findOne({
    translations: { $elemMatch: { language: ??? } }
});
arrays node.js mongodb mongoose
1个回答
1
投票

我认为你正在寻找$nin操作,请尝试:

Model.findOne({
   'translations.language': { $nin: [ "spanish", "french" ] }
});
© www.soinside.com 2019 - 2024. All rights reserved.