在 mongo db 中的文档的多个数组中搜索字符串的存在

Search for the presence of a string in multiple arrays of a document in mongo db

我想进行一个查询,我现在将在其中提供一个 _id 和国家/地区名称,结果我想找到 true 或 false 值,如果给定的国家/地区是否存在于这三个数组中。如果存在则为 true 而不是 false。例如我有 country:France_id:5e4d18bd10e5482eb623c6e4 然后结果将是-

country_array_1:是的, country_array_2:true, country_array_3:false

我想要一个 mongo 数据库查询这种类型的输入和输出我可以是 aggregate 或 findOne 但我想要我的答案

 0  _id:5e4d18bd10e5482eb623c6e4
       country_array_1:['France','Italy','China'],
       country_array_2:['Finland','England','France']
       country_array_3:['USA','turkey','India']

 1  _id:5e4345bd10e5482eb62E4X11f
       country_array_1:['Srilanka','Malaysia','Pakistan'],
       country_array_2:['Egypt','Austria','Netherland']
       country_array_3:['Mexico','turkey','India']

我想进行一个查询,我将在其中输入

[Input]
 _id:5e4d18bd10e5482eb623c6e4,
 country:'France'

[Output]
 country_array_1:true,
 country_array_2:true,
 country_array_3:false

聚合管道

db.test.aggregate([
  { $match: { _id: ObjectId("5e4d18bd10e5482eb623c6e4") } },
  {
    $project: {
      country_array_1: {
        $in: ["France", "$country_array_1"]
      },
      country_array_2: {
        $in: ["France", "$country_array_2"]
      },
      country_array_3: {
        $in: ["France", "$country_array_3"]
      }
    }
  }
]);

输出:

{
    "_id" : ObjectId("5e4d18bd10e5482eb623c6e4"),
    "country_array_1" : false,
    "country_array_2" : true,
    "country_array_3" : false
}