I have created my mongoose user_schema like below
const mongoose = require('mongoose')
const interactions = require('./interaction').schema
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: String,
email: { type: String, unique: true, required: true },
password: { type: String, required: true, },
dob: Date,
address: { type: Schema.Types.ObjectId, ref: 'Address' },
profile_pic: String,
resume: { type: Schema.Types.ObjectId, ref: 'Resume' },
interactions: [interactions]
})
module.exports = mongoose.model("User", UserSchema)
and this is the interaction_schema
const mongoose = require('mongoose')
const helpers = require('../utils/helpers.js')
const Schema = mongoose.Schema;
const InteractionSchema = new Schema({
interaction_type: {
type: String,
enum: helpers.interaction_choices,
default: helpers.interaction_choices.liked
},
interacted_with: { type: String, required: true },
}, {
timestamps: true
})
module.exports = mongoose.model("Interaction", InteractionSchema)
For the interaction_choice, I have used a const variable like this
const interaction_choices = {
sent: 'sent',
liked: 'liked',
rejected: 'rejected',
incoming: 'incoming',
connected: 'connected',
}
module.exports = { interaction_choices }
Now, to add a new interaction I am doing this
async function sendRequest(senderId, recieverId) {
const senderUpdates = {
interaction_type: helpers.interaction_choices.sent,
interacted_with: senderId,
};
const recieverUpdates = {
interaction_type: helpers.interaction_choices.incoming,
interacted_with: recieverId,
};
try {
await User.findByIdAndUpdate(senderId, {
$push: {
interactions: senderUpdates,
},
});
} catch (error) {
console.log("Sender Error : " + error);
return false;
}
try {
await User.findByIdAndUpdate(recieverId, {
$push: {
interactions: recieverUpdates,
},
});
} catch (error) {
console.log("Reciever Error : " + error);
return false;
}
return true;
}
Till now, everything is working as per my expectations. But now I am stuck on removing one specific interaction for a user, based on the interacted_with field of interaction_schema. Please guide me what should I do to do so. Thank you so much.