0

QUERYING MONGODB: RETREIVE SHOPS BY NAME AND BY LOCATION WITH ONE SINGLE QUERY

Hi folks! I'm building a "search shops" application using MEAN Stack.

I store shops documents in MongoDB "location" collection like this:

{
 _id: .....
 name: ...//shop name
 location : //...GEOJson
}

UI provides to the users one single input for shops searching. Basically, I would perform one single query to retrieve in the same results array:

  • All shops near the user (eventually limit to x)

  • All shops named "like" the input value

On logical side, I think this is a "$or like" query

Based on this answer Using full text search with geospatial index on Mongodb probably assign two special indexes (2dsphere and full text) to the collection is not the right manner to achieve this, anyway I think this is a different case just because I really don't want to apply sequential filter to results, "simply" want to retreive data with 2 distinct criteria.

If I should set indexes on my collection, of course the approach is to perform two distinct queries with two distinct mehtods ($near for locations and $text for name), and then merge the results with some server side logic to remove duplicate documents and sort them in some useful way for user experience, but I'm still wondering if exists a method to achieve this result with one single query.

So, the question is: is it possible or this kind of approach is out of MongoDB purpose?

Hope this is clear and hope that someone can teach something today!

Thanks

Mind84
  • 1
  • 3
  • So what exactly are you missing from the super exhaustive answer in the linked SO question? To me, it seems like your case is literally identical...?! – dnickless Dec 09 '17 at 13:34
  • @dnickless hi, thanks for the reply! Nope, this case is a little bit different. The question linked is about filter by location and THEN by name (or viceversa). My question is about getting two indipendent results, results from location and results from name. Sorry if the question is not completely clear – Mind84 Dec 11 '17 at 09:54

0 Answers0