[英]Mongoose find all documents that have a string in an array
[英]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.