簡體   English   中英

Mongoose - 查找數組沒有任何匹配項的文檔

[英]Mongoose - Find documents where array doesn't have any matches

這是我的收藏的架構:

{
    _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: ??? } }
});

我認為您正在尋找$nin運算符,請嘗試:

Model.findOne({
   'translations.language': { $nin: [ "spanish", "french" ] }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM