title: Mongodb - Finding GeoNear within nested JSON objects
body: <p>I need to get the closest place near a certain point using this data structure:</p>
  
  <pre><code>    [
      {
          "data_id": "127",
          "actual_data": [
              {
                  "id": "220",
                  "value": "shaul"
              },
              {
                  "id": "221",
                  "value": "3234324"
              },
              {
                  "id": "222",
                  "value": {
                      "lngalt": [
                          13.7572225,
                          -124.0429047
                      ],
                      "street_number": null,
                      "political": null,
                      "country": null,
                  }
              },
              {
                  "id": "223",
                  "value": "dqqqf1222fs3d7ddd77@Dcc11cS2112s.com"
              },
              {
                  "id": "224",
                  "value": "123123"
              },
              {
                  "id": "225",
                  "value": "lala1"
              },
  ....
      },
      {
          "data_id": "133",
          "actual_data": [
              {
                  "id": "260",
                  "value": {
                      "lngalt": [
                          1.7572225,
                          32.0429047
                      ],
                      "street_number": null,
                      "political": null,
                      "country": null,
                  }
              },
              {
                  "id": "261",
                  "value": -122.25
              }
          ],
      }
  ]
  </code></pre>
  
  <p>I used the following query in order to get what I need:</p>
  
  <pre><code>{
      "actual_data": {
          "$elemMatch": {
              "id": "260",
              "value.lngalt": {
                  "$near": {
                      "$geometry": {
                          "type": "Point",
                          "coordinates": [
                              -73.9667,
                              40.78
                          ]
                      },
                      "$minDistance": 1000,
                      "$maxDistance": 5000
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But after queering it, I get "Can't canonicalize query: BadValue geoNear must be top-level expr" (error code: 17287). It's strange since I do get the right results when I query without the $near but with $elemMatch only in order to get the exact object with particular value.</p>
  
  <p>Thanks.</p>
  
  <p><strong>SOLVED!</strong></p>
  
  <p>for future refarance I used $geoWithin instead $near function. So my query looks like :</p>
  
  <pre><code>{
      "actual_data": {
          "$elemMatch": {
              "id": "260",
              "value.lnglat": {
                  "$geoWithin": {
                      "$centerSphere": [
                          [
                              -71.07651880000003,
                              42.353068
                          ],
                          0.001
                       ]
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>PEACE!</p>