如何在整个文档和 MongoDB 中的所有子文档中搜索值的匹配项?
How to search for a match on values in whle document and all subdocuments in MongoDB?
我有一个 mongo 数据库,其中包含一个名为 store 的集合。
db.store.find()
在 mongo shell 上产生以下结果。
{ "_id" : ObjectId("575d6fab0cde6714290f444f"), "train" : "a", "data" : { "0" : { "this" : 21, "that" : 31, "value" : 11, "month" : 1 }, "3" : { "this" : 21, "that" : 34, "value" : 14, "month" : 2 } } }
{ "_id" : ObjectId("575d6fab0cde6714290f4450"), "train" : "b", "data" : { "1" : { "this" : 22, "that" : 32, "value" : 12, "month" : 2 }, "4" : { "this" : 25, "that" : 35, "value" : 15, "month" : 1 } } }
{ "_id" : ObjectId("575d6fab0cde6714290f4451"), "train" : "c", "data" : { "2" : { "this" : 22, "that" : 33, "value" : 13, "month" : 1 }, "5" : { "this" : 26, "that" : 36, "value" : 16, "month" : 2 } } }
我想在文档中搜索与某些 keys/values 匹配的所有 document/subdocument/field。
我应该如何搜索文档,以便搜索 {'this': 22}
的结果文档如下所示。
{ "_id" : ObjectId("575d6fab0cde6714290f4450"), "train" : "b", "data" : { "1" : { "this" : 22, "that" : 32, "value" : 12, "month" : 2 }, "4" : { "this" : 25, "that" : 35, "value" : 15, "month" : 1 } } }
{ "_id" : ObjectId("575d6fab0cde6714290f4451"), "train" : "c", "data" : { "2" : { "this" : 22, "that" : 33, "value" : 13, "month" : 1 }, "5" : { "this" : 26, "that" : 36, "value" : 16, "month" : 2 } } }
类似地搜索 {'this': 21}
会在下方 return。
{ "_id" : ObjectId("575d6fab0cde6714290f444f"), "train" : "a", "data" : { "0" : { "this" : 21, "that" : 31, "value" : 11, "month" : 1 }, "3" : { "this" : 21, "that" : 34, "value" : 14, "month" : 2 } } }
我知道 db.store.find({'data.0.this':21})
也会产生与上面 {'this': 21}
相同的结果,但这不是我在这里看到的,因为我不知道字段 data
将字段为 this
的字段文档。它可以是 data.0
或 data.1
或 data.2
.
您构建文档的方式将无法进行您想要的查询。我假设嵌入的文档在一个数组中。但是您拥有的是单个嵌入式文档。
如果您更改文档结构使数据键成为存根文档数组,那么您可以进行如下查询:
db.store.find({'data.this':21})
我有一个 mongo 数据库,其中包含一个名为 store 的集合。
db.store.find()
在 mongo shell 上产生以下结果。
{ "_id" : ObjectId("575d6fab0cde6714290f444f"), "train" : "a", "data" : { "0" : { "this" : 21, "that" : 31, "value" : 11, "month" : 1 }, "3" : { "this" : 21, "that" : 34, "value" : 14, "month" : 2 } } }
{ "_id" : ObjectId("575d6fab0cde6714290f4450"), "train" : "b", "data" : { "1" : { "this" : 22, "that" : 32, "value" : 12, "month" : 2 }, "4" : { "this" : 25, "that" : 35, "value" : 15, "month" : 1 } } }
{ "_id" : ObjectId("575d6fab0cde6714290f4451"), "train" : "c", "data" : { "2" : { "this" : 22, "that" : 33, "value" : 13, "month" : 1 }, "5" : { "this" : 26, "that" : 36, "value" : 16, "month" : 2 } } }
我想在文档中搜索与某些 keys/values 匹配的所有 document/subdocument/field。
我应该如何搜索文档,以便搜索 {'this': 22}
的结果文档如下所示。
{ "_id" : ObjectId("575d6fab0cde6714290f4450"), "train" : "b", "data" : { "1" : { "this" : 22, "that" : 32, "value" : 12, "month" : 2 }, "4" : { "this" : 25, "that" : 35, "value" : 15, "month" : 1 } } }
{ "_id" : ObjectId("575d6fab0cde6714290f4451"), "train" : "c", "data" : { "2" : { "this" : 22, "that" : 33, "value" : 13, "month" : 1 }, "5" : { "this" : 26, "that" : 36, "value" : 16, "month" : 2 } } }
类似地搜索 {'this': 21}
会在下方 return。
{ "_id" : ObjectId("575d6fab0cde6714290f444f"), "train" : "a", "data" : { "0" : { "this" : 21, "that" : 31, "value" : 11, "month" : 1 }, "3" : { "this" : 21, "that" : 34, "value" : 14, "month" : 2 } } }
我知道 db.store.find({'data.0.this':21})
也会产生与上面 {'this': 21}
相同的结果,但这不是我在这里看到的,因为我不知道字段 data
将字段为 this
的字段文档。它可以是 data.0
或 data.1
或 data.2
.
您构建文档的方式将无法进行您想要的查询。我假设嵌入的文档在一个数组中。但是您拥有的是单个嵌入式文档。
如果您更改文档结构使数据键成为存根文档数组,那么您可以进行如下查询:
db.store.find({'data.this':21})