Efficiently find users using $near and excluding those who have disliked the user - mongodb

Here is the assumed schema I think is the best to put into place
Users
userId
first_name
age
gender
location (lat,lon)
Matches
userId (voter)
like (vote)
liked_UserId
I know how to find users using the near parameter:
db.users.find({loc: {$near:[-180,40]}}).limit(3)
BUT
1) I'm trying to figure out the best and efficient way of finding users who are the closest and excluding users who have disliked this user. Should I make two query calls??? maybe a $where clause from the other collection somehow?
Edit: I'm thinking make one query to get all the users who have disliked this certain user, then add this array of users as part of a second query in the $nin...but will that be very slow if there are over 400,000 entries?
2) Does Tinder show the user of a profile of another user that disliked them?

Related

Firebase Firestore query by document id using swift

I am trying to query my database to get all the users where the document id is equal to the users id. I know that I can add that id as a field into the document, but I'd like to optimize the memory I take up, so I'd prefer accessing the document id directly through. Something like
.whereField(DOCUMENT_ID, isEqualTo, User.id)
DOCUMENT_ID is not a valid field so far as I know, and I don't know the equivalent. I have read around that other languages modules have a workaround like
.whereField(Firebase.firestore.FieldValues.documentId(), isEqualTo, User.id)
but I've heard hide nor hair of that in swiftUI. Any ideas?
To filter on the document ID in a query, you can use FieldPath.documentID() in Swift too: https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/FieldPath#/c:objc(cs)FIRFieldPath(cm)documentID. But if you do this in a single document, that'd be the same as just doing document(User.id).

What would be the best way to count demographics from DB? Mongoose+MongoDB

I would like to present in my application's admin-pannel a count of the users demographics.
For example:
United States: 500 users
United Kingdom: 241 users
India: 2141241221 users.
Israel: 1 user.
Since there are a lot of countries in the world, I don't want to start searching for each country specifically. I would like to run one search that will count how many times each country appeared. I can't think of an idea that would not require me to search for countries specifically.
Example of what I do NOT want to do:
mongoose.find({Country: India}).then((documents) => { console.log("documents.length")} )
Using this method will require me to write the same line for each country that exists, and I'm sure theres a better way to go about it.
Use Mongodb Aggregation $group stage to achieve this.

Querying MongoDB: retreive shops by name and by location with one single query

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

how to join a collection and sort it, while limiting results in MongoDB

lets say I have 2 collections wherein each document may look like this:
Collection 1:
target:
_id,
comments:
[
{ _id,
message,
full_name
},
...
]
Collection 2:
user:
_id,
full_name,
username
I am paging through comments via $slice, let's say I take the first 25 entries.
From these entries I need the according usernames, which I receive from the second collection. What I want is to get the comments sorted by their reference username. The problem is I can't add the username to the comments because they may change often and if so, I would need to update all target documents, where the old username was in.
I can only imagine one way to solve this. Read out the entire full_names and query them in the user collection. The result would be sortable but it is not paged and so it takes a lot of resources to do that with large documents.
Is there anything I am missing with this problem?
Thanks in advance
If comments are an embedded array, you will have to do work on the client side to sort the comments array unless you store it in sorted order. Your application requirements for username force you to either read out all of the usernames of the users who commented to do the sort, or to store the username in the comments and have (much) more difficult and expensive updates.
Sorting and pagination don't work unless you can return the documents in sorted order. You should consider a different schema where comments form a separate collection so that you can return them in sorted order and paginate them. Store the username in each comment to facilitate the sort on the MongoDB side. Depending on your application's usage pattern this might work better for you.
It also seems strange to sort on usernames and expect/allow usernames to change frequently. If you could drop these requirements it'd make your life easier :D

Mongoid: retrieving documents whose _id exists in another collection

I am trying to fetch the documents from a collection based on the existence of a reference to these documents in another collection.
Let's say I have two collections Users and Courses and the models look like this:
User: {_id, name}
Course: {_id, name, user_id}
Note: this just a hypothetical example and not actual use case. So let's assume that duplicates are fine in the name field of Course. Let's thin Course as CourseRegistrations.
Here, I am maintaining a reference to User in the Course with the user_id holding the _Id of User. And note that its stored as a string.
Now I want to retrieve all users who are registered to a particular set of courses.
I know that it can be done with two queries. That is first run a query and get the users_id field from the Course collection for the set of courses. Then query the User collection by using $in and the user ids retrieved in the previous query. But this may not be good if the number of documents are in tens of thousands or more.
Is there a better way to do this in just one query?
What you are saying is a typical sql join. But thats not possible in mongodb. As you suggested already you can do that in 2 different queries.
There is one more way to handle it. Its not exactly a solution, but the valid workaround in NonSql databases. That is to store most frequently accessed fields inside the same collection.
You can store the some of the user collection fields, inside the course collection as embedded field.
Course : {
_id : 'xx',
name: 'yy'
user:{
fname : 'r',
lname :'v',
pic: 's'
}
}
This is a good approach if the subset of fields you intend to retrieve from user collection is less. You might be wondering the redundant user data stored in course collection, but that's exactly what makes mongodb powerful. Its a one time insert but your queries will be lot faster.