简体   繁体   English

在 mongo 中查找嵌套字段与值匹配的示例文档

[英]Find sample documents in mongo where a nested field matches a value

I have millions of records in collection and here are some sample documents我收集了数百万条记录,这里有一些示例文档

{
    _id: ObjectId("003fjasf0234j03j0349")
    FirstLayer: {
        SecondLayer: {
            Status: {
                9428f:{
                    active: "Active"
                }
            }
        }
    }
}

{
    _id: ObjectId("qg3o4034034nr34343")
    FirstLayer: {
        SecondLayer: {
            Status: {
                9428f:{
                    active: "Active"
                }
            }
        }
    }
}

{
    _id: ObjectId("293je2odjojwe0f23")
    FirstLayer: {
        SecondLayer: {
            Status: {
                00fekw:{
                    active: "Not Active"
                }
            }
        }
    }
}

I want to fetch a few sample documents from monogdb where the active: "Active" .我想从 monogdb 中获取一些示例文档,其中active: "Active" I came up with this:我想出了这个:

db.Collection.aggregate([
  {$match:{"FirstLayer.SecondLayer.Status.9428f.active": "Active"}},
  {$sample:{size: 50}}
  ])

It's working but the issue is the sub document inside Stauts {} have different names for documents (ex: "9428f", "00fekw" in the given sample documents).它可以工作,但问题是Stauts {}中的子文档具有不同的文档名称(例如:给定示例文档中的“9428f”、“00fekw”)。 So I can't hard code the path to "active" field.所以我不能硬编码“活动”字段的路径。

Is there any other way to do it?还有其他方法吗?

I don't think is there any straight way to handle this situation in MongoDB,我认为在 MongoDB 中没有任何直接的方法可以处理这种情况,

You can try,你可以试试,

Approach 1:方法一:

  • $set to add a field Status to assign value of Status after converting it to array in key-value format using $objectToArray $set添加一个字段Status以在使用$objectToArray将其转换为键值格式的数组后分配Status的值
  • $match match Status condition $match匹配状态条件
  • $unset remove Status field because its not needed $unset删除Status字段,因为它不需要
  • $sample your documents $sample你的文件
db.collection.aggregate([
  { $set: { Status: { $objectToArray: "$FirstLayer.SecondLayer.Status" } } },
  { $match: { "Status.v.active": "Active" } },
  { $unset: "Status" },
  { $sample: { size: 50 } }
])

Playground操场

Approach 2:方法二:

Condition match with expression $expr , you can use aggregation operators in condition,条件匹配表达式$expr ,可以在条件中使用聚合运算符,

  • $let to create a vars status to get status array from object and in filter status array and get active status, $let创建一个 vars status以从 object 和过滤器状态数组in获取状态数组并获取active状态,
  • $size get size of above filter result $size获取上述过滤结果的大小
  • expression will check if size is not zero then return result表达式将检查大小是否不为零,然后返回结果
db.collection.aggregate([
  {
    $match: {
      $expr: {
        $let: {
          vars: { status: { $objectToArray: "$FirstLayer.SecondLayer.Status" } },
          in: {
            $size: {
              $filter: {
                input: "$$status",
                cond: { $eq: ["$$this.v.active", "Active"] }
              }
            }
          }
        }
      }
    }
  },
  { $sample: { size: 50 } }
])

Playground操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongo查找子文档匹配多个条件的文档 - mongo find documents where subdocument matches multiple criteria 查找数组中的最大值与查询匹配的所有Mongo文档 - Find all Mongo documents for which the max value in an array matches a query 查找字段值与数组元素匹配的文档 - MongoDB Native Driver - Find documents where field value matches an array element - MongoDB Native Driver 在 mongo 文档中对嵌套字段进行分组 - Group nested field in mongo documents 使用Java查找包含具有特定值的字段的所有mongo文档 - Find all mongo documents containing a field with a specific value using java MongoDB - 查找嵌套文档列表具有多个值大于 0 的字段 - MongoDB - Find where list of nested documents has more than one field with a value greater than 0 在mongo集合的所有文档中插入字段,其中新字段的值是同一文档中另一个字段的值 - Insert fields in all documents of a mongo collection where value of the new field is the value of another field of the same document 如何在mongo的所有嵌套文档中添加字段 - How to add field in all nested documents in mongo 如何查找一个或另一个字段与值匹配的文档? - How to find a document where either one or another field matches a value? 如何查找具有相同字段的mongo文档 - How to find mongo documents with a same field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM