Data modeling for Tinder like app

Hi, i’m new to MongoDB and I’m building a Tinder like app and but i’m not sure what is the best way to model my data. The first approach came to my mind was doing something like this :

    users
        firstname :
        lastname :
       .....
       location :{
       longitude: 46,5412
       latitude: -21.6546  
       }
       matchedUsers : [user1.id, user2.id........]

Because i want to fetch users by their location and also i don’t want any user that is my matchedUsers list.
something like this :

    const users = await User.find({ 
            location: { $nearSphere: { $geometry: { type: "Point", coordinates: [ 0.4539, 49.3784 ] }, $maxDistance: 10000 } },
            matchedUsers: {$ne: req.params.userId}
        })

this works fine but like i said i’m new to MongoDB and i’m worried as my list keep growing. Is this a good way to structure my data or there is a better way?

Hi @Ali_Khodr,

The only problem with this query is that $ne is not a selective operator and cannot utelize indexes.

On the other hand if the geo index can filter out most results I would say this filtering is fine.

I assume that when both users match you will probably create a matching document , consider utilizing Atlas triggers for that :slight_smile:

Edited:
If you are afraid that for some users the list will grow significantly consider using the outlier pattern:

Best
Pavel

Hi @Pavel_Duchovny,

First of all, thanks for your reply. I read the article, it is interesting but unfortunately i don’t think that the outlier pattern could be useful in my use case.

Actually, i didn’t explain well my use case. In my app when a user clicks on the search button, the app will suggest another user to chat with. So there is no this notion of matching. Maybe SuggestedUsers will be better naming than matchedUsers.

So basically, what i’am trying to do is to fetch a nearby user without suggesting the same user twice.

@Ali_Khodr, interesting question…

I think that the way you attack it should be fine however, you cant let this array grow unbound thats a know anti pattern and should be avoided.

Therofore , please consider using $push with $slice and sort to keep the array in reasonable sizes otherwise it will be hard to manage.

Think if you can keep 200-300 users in there and allow some recycling of users for performance reasons.

 users.update(
  { userId: "149064515180820987" },
  {
    $push: {
      SuggestedUsers: {
        $each: [ { userId: "xxx", added: new Date()} ],
  $sort : { added : -1},
        $slice: -300
      }
    }
  }
 )

https://docs.mongodb.com/manual/reference/operator/update/slice/#use-slice-with-other-push-modifiers

Thanks
Pavel

2 Likes

@Pavel_Duchovny

Thanks for your help