0

I'm having a problem querying a dataset called Geolytix Supermarkets January 2015 update The problem is that the queries I've run either return all results or no results at all. This is a snapshot of the dataset:

{"type":"FeatureCollection","features":
[{"type":"Feature","properties":{"OBJECTID":10001,"GLUID":1010010014,"Retailer":"The Co-operative Group",
    "Fascia":"The Co-operative Food","StoreName":"New Road","Add1":"650 Oldham Road",
    "Add2":"Failsworth", "Town":"Manchester","Locality":"Failsworth","Postcode":"M359DU",
    "LongWGS84":-2.14949,"LatWGS84":53.515,"EastingBNG":390185,"NorthingBNG":402050,"PQIflag":2},"geometry":
    {"type":"Point","coordinates":[-2.1494900003123174,53.51499999980253]}},

  {"type":"Feature","properties":{"OBJECTID":10002,"GLUID":1010010015,"Retailer":"The Co-operative Group",
          "Fascia":"The Co-operative Food","StoreName":"Mobberley","Add1":"85 Town Lane",
       "Add2":null,"Town":"Mobberley","Locality":null,"Postcode":"WA167HH","LongWGS84":-2.32905,"LatWGS84":53.3136,
        "EastingBNG":378175,"NorthingBNG":379680,"PQIflag":2},"geometry":
     {"type":"Point","coordinates":[-2.329049999711877,53.31359999998222]}},

I understand that this data structure is embedded/nested documents within arrays and in order to retrieve the data I have to reach into the arrays and documents. To do that I attempted these queries using the dot notation:

db.January_2015.find ({"features.properties.Retailer":"The Co-operative Group"});

This one returns the entire dataset so clearly this is not the correct to reach into it. Then I tried these:

db.January_2015.find ({"features.properties: {"OBJECTID":10001}});

db.January_2015.find ({"features.properties: {"Retailer":"The Co-operative Group}});

db.January_2015.find ({"features.properties": {$elemMatch: {"OBJECTID":10001, "GLUID": 1010010014}}});

These three returned nothing at all. I have studied the documentation and according to it at least one of these approaches should have worked. What am I doing wrong here? I thought about using the aggregation framework but that seemed unduly complex for simple queries such as this. Can someone confirm whether I've tried the correct approach or if I have to use aggregation?

SJ Lovatt
  • 11
  • 4
  • `.find()` does not alter documents in any way. In order to actually "filter" content from arrays and return only matched item(s) you need to apply `$filter` in order to get just that content. For nested arrays as you have, it is not possible to do without aggregation. Hence the marked duplicates that explain the process. – Neil Lunn Oct 18 '17 at 03:33
  • Ok. But since I'm new to Mongo I'm having a hard time applying that to this problem and I don't have a lot of time to solve this (it has already taken up far too much time). I would really appreciate it if you would be kind enough to write out the correct form of the query. – SJ Lovatt Oct 18 '17 at 03:48
  • `.find({ "features.properties.OBJECTID": 10002 }, { "features.$": 1 })` It's not actually nested when the data is viewed clearly. Exact same response is on the linked duplicate. As another matter, if you want to do geospatial queries on this data you would probably be better off importing each "feature" as a separate document. MongoDB does not support `Feature Collection` in GeoJSON, nor is it really straightforward to work with geospatial data inside an array. – Neil Lunn Oct 18 '17 at 03:55
  • Thankyou, that returned the correct result. Is it possible to apply that solution to geospatial queries, if not straightforward? Another thing, I scrutinized the official documentation about querying nested arrays and it made no mention of what you told me. Doesn't that mean that it's incorrect? – SJ Lovatt Oct 18 '17 at 04:10
  • You mean this documentation [Project Specific Array Elements in the Returned Array](https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/#project-specific-array-elements-in-the-returned-array) which does appear to point to the documentation of all the relevant operators as far as I can see. For other cases, if you have a new question then [Ask a new Question](https://stackoverflow.com/questions/ask) and do actually search because there are a lot of answers here already and just a few keywords should have pointed you to the top marked duplicate. – Neil Lunn Oct 18 '17 at 04:22

0 Answers0