Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
Friend Request System with Express & MongoDB

I am trying to let users send friend requests to other users similar to that of Facebook and other social media platforms. I have started creating this functionality, but quickly got stuck since I am new to mongoDB and the whole Schema thing.

Here are the models I currently have:

// User Schema
var UserSchema = new Schema({
    _id: {
        type: Number
    },
    name: {
        type: String
    },
    username: {
        type: String,
        index: true
    },
    password: {
        type: String,
        required: true
    },
    email: {
        type: String
    },  
    friends: [{
        friendName: {
            type: Schema.ObjectId,
            required: true,
            ref: 'User'
        },
        duration: {
            type: Number
        }
    }]
});


// Friendship Schema
var FriendshipSchema = new Schema({
    participants: [{
        type: Schema.Types.ObjectId, 
        ref: 'User'
    }],
    requestTo: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    accepted: {
        type: Boolean, 
        default: false
    },
    user: {
        type: Schema.ObjectId, 
        ref: 'User'
    }
});

var Friendship = module.exports = mongoose.model('Friendship', FriendshipSchema);

var User = module.exports = mongoose.model('User', UserSchema);

This is as far as I have gotten. From here, I do not know how to use these schemas to establish friendships between 2 users. My ultimate goal is to have a button on a webpage that sends a friend request to the intended recipient, where they can then accept or deny the request.

Any help with this would be awesome, since I do not know what to do from here with these 2 schemas. Thanks!

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

We would need to take one schema only which is userSchema(as is Israel said above, you only need an array/object to list your friendship on the userSchema). But we will need to add another schema(said it friendRequestSchema).

FriendRequest schema would be: - ID user request (int) - ID user recipient (int) - status (int) //let say 1= requested, 2=accepted, 3=rejected

And the controller it should be from the user A click "Friend Request" button on your user B page.

  1. Friend Request Button will call a function let saying it "sendFriendRequest()"

If function running it would be recorded on friendRequest DB, which is will record ID of user A(as requester), ID of user B and request status.

If request status = 1 then user B will be notified and give him two option which is accepted and rejected.

  1. User B accept or reject it

If user press button accept, then the status updated in friendRequest DB to be=> 2 (Accepted). Then, you have to call another function to add user ID A to friendship list of User B. Conversely. Then if you want to make a notification you can call it as well.

Else user B will press reject (status will be => 3) then notif it.

UserSchema

var UserSchema = new Schema({
name: String,
username: {
    type: String,
    index: true
},
password: {
    type: String,
    required: true
},
email: String,  
friendship: [String] });

Then FriendRequestschema

var FriendRequestSchema = new Schema({
requester: {
    type: int,
    required: true
},
recipient: {
    type: int,
    required: true
},
status: 
    type: int,
    required: true });

This just to let you know, how its work. More complex method about (sendrequest,block .etc) you can check this link, It's flow process for PHP, but you can easily move it to your js. Hope it help you man.

about 4 years ago · Santiago Trujillo Report

0

Your model can be improved, and your code can be cleaned:

First, you don't need the brackets if you only give type for the field:

var UserSchema = new Schema({
    name: String,
    username: {
        type: String,
        index: true
    },
    password: {
        type: String,
        required: true
    },
    email: String,  
    friends: [String]
});

This should be a simplified version of your schema. The _id field doesn't need to be specified because mongoose creates it automatically. If you wanna put a customized value there, just do it when you insert.

Second:

If you wanna reference other users, why not to use only a simple array that contains ids from other users. For example, if you have user A, the "friendship" of this user are user ids contained in his "friends" field.

{id:12345, username:"A", password:***, email:"a@fakemail.com", friends:[B_id,C_id,D_id]}

In that case, whenever you wanna make a list of friends of A, you can just perform a $lookup operation in mongodb and it will fill the other users information for you.

I don't think I covered all of your questions, but I hope my answer was helpful.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!