So I've run into this issue before but now I really want to flatten it out and never run into it again. I have a user schema which accepts other users as friends.
However, instead of just adding a 'friends' key to the User schema with a single object of type: Schema.Types.ObjectId, ref: "User" , I want to include other properties such as friendship status, etc. Check the code below.
UserSchema = new Schema({
email: {
type: String,
unique: true
},
name: {
type: String,
min: [1, "Name must be at least 1 character in length"],
max: [18, "Name cannot exceed 18 characters in length"]
},
password: {
type: String,
min: [6, "Password must be at least 6 characters in length"],
max: [18, "Password cannot exceed 18 characters in length"]
},
friendships: [
{
friend: {
type: Schema.Types.ObjectId,
ref: "User"
},
myGain: {
type: Number,
default: 0
},
myDebt: {
type: Number,
default: 0
},
status: {
type: String,
default: "pending"
},
requestor: {
type: Number
}
}
]
});
As you can see, friendships are now an array, with each object containing five properties, the first being 'FRIEND'. This is the actual reference to the User model, which will contain another user.
My issue is it starts to confuse me when doing things like adding a friend.
In my controller, here is how I am creating a friendship.
const friendshipController = {
create: function(req, res) {
User.findByIdAndUpdate(req.params.id,
{ $push: { "friendships": {"friend": req.params.friendId } } }, {upsert: true}, function(err, user) {
if (err) {
console.log(err);
} else {
User.findByIdAndUpdate(req.params.friendId,
{ $push: { "friendships": {"friend": req.params.id } } }, {upsert: true}, function(err, user) {
if (err) {
console.log(err);
} else {
console.log("donezo");
}
}
)
}
} )
}
}
The problem is when I look at the new records in mongo, it is the following:
password" : "password",
"bets" : [ ],
"friendships" : [
{
"friend" : ObjectId("581676fbfbbb9a43ac8c979d"),
"_id" : ObjectId("58168257a7c4cb4512b1cafb"),
"status" : "pending",
"myDebt" : 0,
"myGain" : 0
}
],
"__v" : 0
What is that extra _id value underneath friend? What is that referring to? It is a unique value in that when I look at the other friend who had the friendship created, that ObjectId value is a little bit different. The actual ObjectId value of the "friend" property is the only one I want. That is the correct value. That value (ending in 979d) is referencing the other friend. I just don't get what that other _id value is.
Thanks