Hello and welcome back.
Now we want to be able to get the followers and the followings from the database.
So if I refresh this, we can see in the database, in the follower collection, I have the documents
with these properties.
We've already created the method to retrieve followers with the lists.
We want to do the same for the MongoDB database.
So what we are going to do is for a particular user, if we want to get the followers for that particular
user, then we are going to find all documents.
We're the following ID because if a user is being followed and the user wants to get all the followers,
then we find all documents where the following ID matches that particular user's ID.
And then if you want to get all the users that a particular user is following, then we get the documents
where the user's ID matches the follower ID.
So that is what we are going to do to be able to get the data we want from the MongoDB database.
So I will start with that.
So I will go to services DB and follower service.
So I'm going to create two methods.
I'm going to look similar, but we're just some little differences on the type of data we are returning.
So the first one, I'll say public async gets following data or following whichever one you want.
And here I'm going to pass or use the objects ID and it's going to be of type objects ID.
This object ID is coming from MongoDB right here and this is going to return a promise of type I follower
data.
List right here.
This is supposed to be public and we are going to use MongoDB aggregates.
So the first thing to do is, I'll say, followed by a call to await follow our model, those aggregates
we pass in an array of square brackets.
And the first property is going to be an object that takes in the match.
Operator And we want to match every document because for this particular user, we want to get all the
following documents.
So we're going to find every document that matches the follower.
So here for one common user objects ID.
So just like how I explained here.
So if we want to get the all users that are following a particular user, then we find all documents
that matches this full ID.
And then if want to get all the users that a particular user is following where we match with this follower
ID and that's what we are doing right here.
And then the next thing is, remember, we have the.
What's ID right here.
So we want to use the lookup operator.
So we're going to use the lookup operator because we need.
The I follow follower data to be the return type.
So we need the to use the lookup because we need the avatar caller, we need the user name and you ID.
So I think we already used that before.
So let's just take a look if we go to you use the service.
Yes.
So something like this is what we want to do.
But first.
If I go back to Fallujah collection now, you can see this is an ID.
So we need first need to look up the photo I.D. to get the object.
And then in the object we do look up to get the ID, so I can just copy this to.
Right here.
And then pastes.
But this time the collection is going to be user.
And then they look our field.
They look our field is going to be full of ID.
That is this is the local field that we're talking about.
And then.
The foreign field is the ID.
This is the foreign field right here.
And the user collection.
This is the foreign field.
And then we want to return the documents with the key code for we ID.
And I'll change this here to follow.
Like I said, you can set any name to this and then that is what we are going to use here.
And if you want to get to the specific item, you're going to use these dots, then the name of the
field.
So just as a side notes, so this is the first lookup, this will return the this return an object like
this with all these properties, but we still don't have the avatar caller username ID, so we are going
to do another look up to get the ots id.
So I will just copy this again and then paste.
Now this this time is going to be odds from and then.
Now remember that we are getting.
This will become an object.
We getting an object?
So we want the property.
Thought I'd inside the object.
So the look of it is going to be following ID, which is this property we have here.
Then dots, ods id.
And then the foreign key or the foreign field is the ID and if we are returning the object as ops ID
and then here I'm just going to unwind this because look up returns and lists with unwind is going to
return it in its original form.
If it's an object, it will return it as an object.
Okay.
Now, what I want to do is I want to create new fields.
I think we already used that somewhere.
Let me see.
I think we use.
Let me see if I can get that.
If we already use the add fields.
Okay.
Maybe.
Maybe we've not used that.
Okay.
We've not used that.
So we want to use a property to so that we can be able to return the these objects.
And we can do that by using another operator.
And the operator is called Add Fields.
So if you want to create new properties, you use the ADD Fields.
Operator And this we want to add a field to an object and we want to return.
Let me move this to the right right here.
So we want the ID, so on the score ID and we're going to get the ID.
From Fallujah I'd on the school ID so note this ID here is the user ID and we're getting it from this
lookup results which is this Fallujah ID right here to the ID.
So because we use this online operator, that is where we can get the data this way.
Otherwise there's some other operators.
If it's an array, then there's some other operators you can use to get the item.
But this is much better.
And then we need a user name and the user name is going to be from the OTS lookup, not the user.
So this case is going to be the symbol or ID.
Outside Dot's username.
Remember that the user name is coming from the outside, not the follower or following ID.
And then let me add some other properties.
Our duplicates.
This this is going to be about a column right here.
And then the next one is going to be the post count.
But post count is going to come from following ID, so I'll say post.
Or post counts.
I think it's called post count
and then it's going to be follow we ID dots posts counts
and I have the.
Follow us.
Okay.
Here and here.
And then the following counts.
Here and here as well.
And then we need the profile picture.
Okay.
And then what else?
I think we need the UID.
So the UI will come from the outside.
So this is going to be your ID right here.
And then we can add this.
Let's leave this out.
I think I don't think we really need this.
Yeah, we added it while getting the data from the cache, but I can't remember right now if we need
this on the client side.
Well, we can add it.
If we don't need it, maybe later we can take it out.
So this is going to be user ID and then it's going to be.
Louis ID right here.
So if we don't need this later, we can take it out.
So these are the properties that we need.
And then we need to remove the original fields.
So we want to remove this.
We don't want to return these fields.
We only want to return the new fields that we created.
So while I don't want to return this the this underscore underscore.
Ve created that the ID follow.
I don't return those properties.
I can remove them by using the project.
So I'll say project and inside the object ID I'll set it to zero.
Because of this lookup, a new property was added called ID.
We've already gotten what we want from the ID so we don't need it anymore.
We can remove it.
Then the next is going to be the follower.
ID set it to zero follow we id set it to zero.
I don't need to create ID art.
Set it to zero and then I don't need the underscore underscore v set it to zero as well.
So all of these properties will be removed from the final result.
These are the properties that will need these new ones that we created using these ADD fields.
Operator And then what we just need to do is return this property right here or just return Louis right
here.
So this is the method to get the following data.
I can duplicate this and use it to get the follower data as well.
And then we just make some minor changes.
So the function called for.
Right.
It takes an object idea as well.
But now in this case, we want to match documents.
We have the full ID.
So if we want to get all the follow ups for a particular user, we look for the document where they
follow.
We matches the ID.
So this user where the ID matches for we ID, we want to get all the followers.
I think this needs to be full with ID and then this also needs to be follower ID.
Yes, because that's what we have right here.
And then here we just change from following to follow up.
So this is going to be.
Just change all these to follow.
Right here.
I'll change this to follow up as well and return the follower as the final.
Results.
Okay.
I think here we need to set the C.
Let me import this.
So I need to import it.
Set the type to this and here as well.
So these are the two methods that we need.
We have the methods to get from the cache, and these are the methods that will be used to get from
the database.