下面是我收集的模式:
{
_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" ] }
});