1

I am new to the backend and trying to learn by building some stuff but unfortunately, I got stuck. I want to know if I can update a nested array of objects in Users Schema using Mongoose in an efficient and elegant way.

Users Schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    username: {
        type: String,
        required: true,
        unique: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    gender: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },

    friends: [{}],

    notifications: []
    
}, {timestamps: true});

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

In the friends' field, I stored friend request with the status of pending

I want if the user whose the request was sent to, hits an endpoint, to accept the request by changing the status from pending to success.

This is how a friend request was stored:

  friendRequest = {
        _id: req.user.id,
        status: 'pending',
        sentByMe: false,
        new: true,
        inbox: []
    }
   

Thanks as you help me out!!! 🙏🙏🙏

1 Answer 1

1

You should first create an additional friendRequest and inbox schemas like this:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

const InboxSchema = new Schema({
user_id: {
    type: String,
    required: true
},
from_id: {
    type: String,
    required: true
},
message: {
    type: String,
    required: true
},
the_date_time: {
    type: Date,
    required: true
}   
});

mongoose.model('Inbox', InboxSchema);

const FriendRequestSchema = new Schema({
user_id: {
    type: String,
    required: true
},
status: {
    type: String,
    required: true
},
sentByMe: {
    type: Boolean,
    required: true,
    unique: true
},
inbox: [InboxSchema]
})

mongoose.model('FriendRequests', FriendRequestSchema);

and update your Users schema:

const UserSchema = new Schema({
name: {
    type: String,
    required: true
},
username: {
    type: String,
    required: true,
    unique: true
},
email: {
    type: String,
    required: true,
    unique: true
},
gender: {
    type: String,
    required: true
},
password: {
    type: String,
    required: true
},

friends: [FriendSchema],

notifications: [FriendRequestSchema]

}, {timestamps: true});

And then use the friendRequest object

friendRequest = {
    _id: req.user.id,
    status: 'pending',
    sentByMe: false,
    new: true,
    inbox: []
}

to update the Users collection

Users.update({ _id: user_id }, { $push: { notifications: friendRequest } });

Whenever you have arrays of objects within collections, its best to define additional schemas. You should also consider adding indexes to your collection schemas.

Update:

A FriendSchema would look like this:

const FriendsSchema = new Schema({
friend_id: {
  type: String,
  required: true
},
friend_name: {
  type: String,
  required: true
},
friendship_made: {
  type: Date,
  required: true
}

// you have to define FriendSchema before you define Users since you 
// now reference [FriendSchema] in UserSchema
mongoose.model('Friends', FriendSchema);

And so is personA friends with personB?

Users.findOne({ "_id": personA.id, "friends.friend_id": personB.id});
Sign up to request clarification or add additional context in comments.

7 Comments

Hi, Thank you for reaching out!! But this is not what I was looking for. I am able to push the request to the friends' array. My question here is how to update the request status after it is been accepted. from 'pending' to 'success' because I initially pushed the request with the status of pending. Thanks.
Do you have a FriendRequest collection where this is stored? Having a separate collection that stores friend requests would be a good design. If you do have a separate collection for friend requests, post the schema here and I'll see if I can offer some code to update it.
No, I don't have a friend request collection. Just a notification field which stores both the request and the accepted ones. But I want to store the friend request in the user friends field so that I can let I let the user know how many requests he's sent out by filtering it to === 'pending' && sentByMe === true, if need be.
I have updated my original answer according to your clarifications. Please let me know if it works for you.
(You should also have a FriendsSchema defined -- but I'll leave that to you!)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.