本文介绍了Mongodb-查询深度嵌套的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在SO和Google上提出了类似的问题,持续了两天。这是文档I have poured over similar questions as this on SO and Google for 2 days. Here is the document{ "rows" : [ { "company" : "1 Gables Inn Bed & Breakfast", "address" : " 318 Quapaw Avenue Hot Springs", "a_url" : " AR 71901 Phone: 501-623-7576 ", "json" : { "results" : [ { "address_components" : [ { "long_name" : "318", "short_name" : "318", "types" : [ "street_number" ] }, { "long_name" : "Quapaw Avenue", "short_name" : "Quapaw Ave", "types" : [ "route" ] }, { "long_name" : "Hot Springs", "short_name" : "Hot Springs", "types" : [ "locality", "political" ] }, { "long_name" : "Hot Springs", "short_name" : "Hot Springs", "types" : [ "administrative_area_level_3", "political" ] }, { "long_name" : "Garland County", "short_name" : "Garland County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Arkansas", "short_name" : "AR", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "71901", "short_name" : "71901", "types" : [ "postal_code" ] } ], "formatted_address" : "318 Quapaw Avenue, Hot Springs, AR 71901, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 34.508227, "lng" : -93.05744779999999 }, "southwest" : { "lat" : 34.50822, "lng" : -93.05746420000001 } }, "location" : { "lat" : 34.508227, "lng" : -93.05746420000001 }, "location_type" : "RANGE_INTERPOLATED", "viewport" : { "northeast" : { "lat" : 34.5095724802915, "lng" : -93.05610701970851 }, "southwest" : { "lat" : 34.5068745197085, "lng" : -93.05880498029151 } } }, "types" : [ "street_address" ] } ], "status" : "OK" } }, { "company" : "1000 Acres Retreat House", "address" : " 122 Russell Street Hot Springs ", "a_url" : " AR 71901 501-318-4117 Website ", "json" : { "results" : [ { "address_components" : [ { "long_name" : "122", "short_name" : "122", "types" : [ "street_number" ] }, { "long_name" : "Russell Street", "short_name" : "Russell St", "types" : [ "route" ] }, { "long_name" : "Hot Springs", "short_name" : "Hot Springs", "types" : [ "locality", "political" ] }, { "long_name" : "Hot Springs", "short_name" : "Hot Springs", "types" : [ "administrative_area_level_3", "political" ] }, { "long_name" : "Garland County", "short_name" : "Garland County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Arkansas", "short_name" : "AR", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "71901", "short_name" : "71901", "types" : [ "postal_code" ] } ], "formatted_address" : "122 Russell Street, Hot Springs, AR 71901, USA", "geometry" : { "location" : { "lat" : 34.5313788, "lng" : -93.05564799999999 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 34.5327277802915, "lng" : -93.05429901970848 }, "southwest" : { "lat" : 34.5300298197085, "lng" : -93.0569969802915 } } }, "types" : [ "street_address" ] } ], "status" : "OK" } } ]}如果你是我想知道,这些行来自Google地理编码结果I case you are wondering, these rows are from Google geocoding result我想做的是找到并提取每个地址的 postal_code。 postal_code的位置因请求而异,因此我们不能依赖数组中固定的预期位置。相反,我们必须识别等于 postal_code的类型,然后提取相应的 long_name。最终, postal_code回到了MySQL,所以我想这里的目的是整理文档。What I am trying to do is locate and extract the "postal_code" for each address. The position of the "postal_code" can vary from request to request, so we cannot rely on a fixed, expected position in the array. Instead we must identify the "types" equal to "postal_code", then extract the extract the corresponding "long_name". Ultimately that "postal_code" is going back into MySQL, so I guess that objective here is to "flatten" the document.万一它对任何人都有帮助,这就是记录看起来像在Mongodb中一样:In case it helps anyone, here is what a record looks like in Mongodb:每个记录都在其自己的数组行中,这似乎使查询变得复杂,因为我尝试了以下内容的变体:Each record is in its own array row, which seems to complicate the query, in that I have tried variations on the following:db.AR.find({ 'results.address_components.types': { $in: ["postal_code"] } }).toArray()db.AR.find({ 'rows.results.address_components.types': { $in: ["postal_code"] } }).toArray() ...并且似乎无法获得任何结果。没事,Zilch。 Nada。...and cannot seem to get any results reutnred. Nothing, Zilch. Nada. btw,这是我对MongoDB的第一次见解,因为它似乎是最适合此类问题的工具。我还尝试了MySQL json_extract UDF函数,但我似乎也没有取得任何进展。因此,我想我要说的是,如果我使这一工作变得比原本需要的困难得多,并且有人有更好的方法,那么我愿意接受建议。btw, this is my first effor with MongoDB since it seems to be the tools most suited to this type of problem. I have also tried the MySQL "json_extract" UDF functions, but I can't seem to make any headway with that, either. So I guess what I am syaing is that if I am making this more difficult than it needs to be--and someone has a better approach--I am open to suggestions.无耻推荐答案您真正想要的是聚合框架,它不仅用于聚合结果,而且还用于文档重塑,这是您的问题的一部分。您也没有按要求查询正确的路径What you really want here is the aggregation framework, which is not only for "aggregating" results but it also for "document reshaping", which is part of your question. You were also not querying by the correct path as is requireddb.AR.aggregate([ { "$unwind": "$rows" }, { "$unwind": "$rows.json.results" }, { "$unwind": "$rows.json.results.address_components" }, { "$match": { "rows.json.results.address_components.types": "postal_code" }}, { "$project": { "company": "$rows.company", "address": "$rows.address", "postal_code": "$rows.json.results.address_components.long_name" }}])为您提供结果:{ "_id" : ObjectId("5383eebcdef83b11e3d71ec4"), "company" : "1 Gables Inn Bed & Breakfast", "address" : " 318 Quapaw Avenue Hot Springs", "postal_code" : "71901"}{ "_id" : ObjectId("5383eebcdef83b11e3d71ec4"), "company" : "1000 Acres Retreat House", "address" : " 122 Russell Street Hot Springs ", "postal_code" : "71901"}由于文档包含嵌套数组,因此必须 $ unwind 所有这些,然后再使用> $ match 将结果过滤到最里面的数组中 types 字段中包含 postal_code的结果。 $ unwind 语句对文档进行非规范化,实际上是为每个数组条目创建一个文档副本。As the document contains nested arrays it is necessary to $unwind all these before you use $match to filter the results to those that contain "postal_code" in the types field in the innermost array. The $unwind statements "de-normalize" the document, essentially creating a document "copy" for each array entry.最后,您可以使用 $ project 将文档重塑或展平为所需的结果样式。Finally you can use $project to re-shape or "flatten" the document into the style of result you want.您还可以在聚合框架运算符。 这篇关于Mongodb-查询深度嵌套的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 18:34
查看更多