MongoDB request: $ next to aggregation

I have a collection that looks like this

{ "_class" : "User", "_id" : "id1", "places" : [ { "_id" : "1", "address" : "test1", "location" : { "latitude" : 1, "longitude" : 1 } }, { "_id" : "2", "address" : "test2", "location" : { "latitude" : 2, "longitude" : 2 } },... ] } 

I try to get every user location (within 2 km). This request does not work:

 db.users.ensureIndex({"places.location":"2d"}) db.users.aggregate([ {$match : { "_id" : "id1" } }, {$unwind : "$places"}, {$project:{_id:0, places:1}, {$match : {"places.location" : { $near : { $geometry : { type : "2d" , coordinates : [ -1 , -2 ] } }, $maxDistance : 2000 } } }]) Error: Printing Stack Trace at printStackTrace (src/mongo/shell/utils.js:37:15) at DBCollection.aggregate (src/mongo/shell/collection.js:897:9) at (shell):1:10 JavaScript execution failed: aggregate failed: { "errmsg" : "exception: $near is not allowed inside of a $match aggregation expression", "code" : 16424, "ok" : 0 

I do not know how to use $ near with aggregation.

When i try

 db.users.find( { "places.location" : { $near: { $geometry : { type : "2d" , coordinates : [ -1 , -2 ] } }, $maxDistance : 2000 } } 

It returns me n times for a given user (n = each user location), so I decided to use aggregation, but I was stuck.

thanks in advance

EDIT

I tried $ geoNear:

 db.users.aggregate( {$geoNear : { near: [-1, 1], distanceField: "distance", query : {"_id" : "id1"}, uniqueDocs: true, maxDistance : 2000 // In the specs, distance has to be in radians }}) 

and he returns me the entire user (with all places, even distant ones). A.

Therefore, I need to redo the document before looking for the right places.

  • Get the correct user (match)
  • Untie all user places
  • then search all places in the 2 km range (near / geonear).
  • change the result (project)

The result I expect looks something like this:

 "result" : [ { "_id" : "1", "location" : { "latitude" : 1, "longitude" : 1 } }, { "_id" : "2", "location" : { "latitude" : 2, "longitude" : 2 } } ]} 

The problem is that I can not use other steps before $ geoNear, my search parameters should be specified in the "query" field. I get the following error:

 $geoNear is only allowed as the first pipeline stage 
+8
source share
1 answer

You will have to use $ geoNear , and only if you use V2.4 and higher

 db.users.aggregate( {$geoNear : { near: [-1, -2], distanceField: "distance", query : {"_id" : "id1"}, uniqueDocs: true, maxDistance : 2000 }}) 

Change: after editing the question

The query below will give you location and distance but not retrieve the internal (array element) _id

 db.users.aggregate( {$geoNear : { near: [-1, -2], distanceField: "distance", includeLocs: "location", query : {"_id" : "id1"}, maxDistance : 2000 }}, {$project : {"location" : 1, "_id" : 0, "distance" : 1}} ) 

Note the addition of includeLocs and the exception of uniqueDocs: true

To also extract the internal _id, you have to (after this example) expand and conditionally project or so, but I don't think it's worth it unless you need an address instead of _id

+7
source

All Articles