简体   繁体   English

MongoDB 中的完全匹配文档

[英]Exact match document in MongoDB

I am currently using MongoDB's driver on a Java application.我目前在 Java 应用程序上使用 MongoDB 的驱动程序。 Suppose I have the two following documents stored in a collection:假设我将以下两个文档存储在一个集合中:

Document A:文件 A:

{
  "_id": "something",
  "key1": "Value1",
  "Key2": "Value2",
  "Key3": "Value3"
}

Document B:文件 B:

{
  "_id": "somethingDifferent",
  "key1": "Value1",
  "Key2": "Value2"
}

Now, I want to retrieve an exact match of DocumentB's from the collection without also returning Document A, by using key1 and key2's values, and not _id since I don't know it beforehand.现在,我想通过使用 key1 和 key2 的值​​而不是 _id 从集合中检索 DocumentB 的完全匹配,而不返回 Document A,因为我事先不知道它。

If I just use DocumentB as a query (without _id), Mongo will also return DocumentA since it has matches all the keys and values, disconsidering the fact that DocumentA has an extra key (key3).如果我只使用 DocumentB 作为查询(没有 _id),Mongo 也会返回 DocumentA,因为它匹配所有的键和值,忽略 DocumentA 有一个额外的键(key3)的事实。

Is there any way to perform this "exact match" query in Mongo?有没有办法在 Mongo 中执行这个“完全匹配”查询? Will I have to implement a check afterwards?之后我需要进行检查吗?

Assuming by "exact match" you mean the document has no other fields but the ones in the query.假设“完全匹配”是指文档除了查询中的字段之外没有其他字段。

You will need to use aggregation framework to retrieve a list of keys in the object and filter out the ones that have unexpected keys.您将需要使用聚合框架来检索对象中的键列表并过滤掉具有意外键的键。 In mongodb syntax it can be like this:在 mongodb 语法中,它可以是这样的:

db.collection.aggregate([
  {
    $match: {
        "key1": "Value1",
        "Key2": "Value2"
    }
  },
  {
    $addFields: {
      extra_keys: {
        $setDifference: [
          {
            $map: {
              input: {
                $objectToArray: "$$ROOT"
              },
              in: "$$this.k"
            }
          },
          [                   // <== the list of keys in the query
            "_id",
            "key1",
            "Key2"
          ]
        ]
      }
    }
  },
  {
    $match: {
      extra_keys: []
    }
  }
])

If your matching query key:value pair has the same order than your stored documents, the aggregation below may work with $eq operator .如果您匹配的查询key:value对与您存储的文档具有相同的顺序,则下面的聚合可能适用于$eq运算符

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $eq: [
          {
            "key1": "Value1",
            "Key2": "Value2"
          },
          {
            $arrayToObject: {
              $filter: {
                input: {
                  $objectToArray: "$$ROOT"
                },
                cond: {
                  $ne: [
                    "$$this.k",
                    "_id"
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
])

MongoPlayground Mongo游乐场

Warning: These objects are equals警告:这些对象是相等的

{                          {
  "key1": "Value1"   ----      "key1": "Value1",
  "Key2": "Value2",  ----      "Key2": "Value2"
}                          }

These objects are not equals这些对象等于

{                          {
  "key1": "Value1"   --/-      "Key2": "Value2",
  "Key2": "Value2",  -/--      "key1": "Value1"
}                          }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM