$geoNear(aggregation) AFTER a search(aggregation) mongo DB

i want to make a $geoNear operation to the results of a $search operation to list the results in order from nearest to farthest. the order of the operations are important because i first get the items and then $lookup for there location fields in another collection which is required for the geoNear operation.

Hi @Rahman_Colak,

Welcome to the community :wave:

Could you please provide the following:

  1. Any current aggregation pipeline commands you’ve attempted so far
  2. A sample output of the aggregation’s you attempted
  3. The expected output documents
  4. Further context regarding the usage of this pipeline (use case / scenario)

Please note that $geoNear must be the first stage of your pipeline as noted in the documentation.

Regards,
Jason

Hello Jason,

what i try to do is: i want to make a autocomplete search and with the results i want to list them by nearest to farthest. first i have to make a lookup with this results to get the needed coordinates so i cant use geoNear as first or make a near operation in an compound with search.

The other way to reach this would be i make first the geoNear but how can i make after that a autocomplete search to the results to filter only the matches

maybe the simplest way is to update the schema for the usecase.

Regards,
Rahman

Hi @Rahman_Colak,

Thanks for getting back to me with those details.

first i have to make a lookup with this results to get the needed coordinates so i cant use geoNear as first or make a near operation in an compound with search.

Do you have sample documents that you could provide here that you are using for the $lookup ?

The other way to reach this would be i make first the geoNear but how can i make after that a autocomplete search to the results to filter only the matches

$geoNear and $search cannot be used together in a single aggregation pipeline as they each are required to be the first stage of the pipeline.

maybe the simplest way is to update the schema for the usecase.

This could be the case but I’m not entirely certain of how the documents in your collections are structured. If you can provide the information I asked for in my previous post I may be able to provide more specific recommendations.

You may find the following post helpful: Searching on Your Location with Atlas Search and Geospatial Operators

Regards,
Jason

Hey Jason,

Thank you for your prompt reply. The post which you attached: i try the same way, but if you see my schema you will understand why i can not do it like that.

so i have a box collection(with 2dsphere index) with document structure

  • _id
  • name
  • location
  • itemList

and i have the item collection(with atlas search index) with document structure

  • _id
  • name
  • boxId

every item belong to a box. I want to search for items and order the results by

As you said:

$geoNear and $search cannot be used together in a single aggregation pipeline 
as they each are required to be the first stage of the pipeline.

Maybe i need a way to query over the results “on the fly” but then i have no index i think…

best Regards,

Rahman

Hi @Rahman_Colak,

Thank you for providing the box and item collection structures.

With the current schema design you’ve prompted, you won’t be able to perform the $geoNear and $search autocomplete stages in a pipeline as I have suggested in my previous post.

Perhaps you could further narrate the use case from the user’s point of view to see if there may be an alternative solution / approach. Additionally, please provide sample document(s) from each collection.

Regards,
Jason

1 Like