gpt4 book ai didi

node.js - MongoDB - 找到至少一个数组值丢失的地方

转载 作者:可可西里 更新时间:2023-11-01 09:57:36 26 4
gpt4 key购买 nike

我想找到数组中至少有一个值缺失的所有文档。例如:

数组:["spanish", "dutch", "french"]

选择以下文档:

{
translations: [{ language: "spanish" }]
}
//and
{
translations: [{ language: "spanish" }, { language: "french" }]
}
//and
{
translations: [{ language: "german" }]
}

但这些应该被选中:

{
translations: [{ language: "spanish" }, { language: "french" }, { language: "dutch" }]
}
//and
{
translations: [{ language: "spanish" }, { language: "french" }, { language: "dutch" }, { language: "german" }]
}

最佳答案

您可以使用$setIntersection 来查找交集$size 小于三个的文档

{$expr : 
{$lt :[
{$size :{$setIntersection : ["$translations.language", ["spanish", "dutch", "french"]]}},
3
]}
}

收藏

> db.t80.find()
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cc7"), "translations" : [ { "language" : "spanish" } ] }
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cc8"), "translations" : [ { "language" : "spanish" }, { "language" : "french" } ] }
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cc9"), "translations" : [ { "language" : "german" } ] }
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cca"), "translations" : [ { "language" : "spanish" }, { "language" : "french" }, { "language" : "dutch" } ] }
{ "_id" : ObjectId("5c68552ac6f8be1a888e6ccb"), "translations" : [ { "language" : "spanish" }, { "language" : "french" }, { "language" : "dutch" }, { "language" : "german" } ] }
>

结果

> db.t80.find({$expr  : {$lt :[{$size :{$setIntersection : ["$translations.language", ["spanish", "dutch", "french"]]}},3]}})
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cc7"), "translations" : [ { "language" : "spanish" } ] }
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cc8"), "translations" : [ { "language" : "spanish" }, { "language" : "french" } ] }
{ "_id" : ObjectId("5c68552ac6f8be1a888e6cc9"), "translations" : [ { "language" : "german" } ] }
>

关于node.js - MongoDB - 找到至少一个数组值丢失的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54726226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com