i have one mongodb collection which have documents like these

now I want to push a value to rooms array of a document where email is [email protected] .... I have made this controller in node js.
module.exports.createroom = async (req,res) => {
const {roomid,email} = req.body;
UserModel.updateOne(
{ email: email },
{
$push: { rooms: roomid}
}
)
res.send('done')
}
user schema
const mongoose = require("mongoose")
const userSchema = new mongoose.Schema({
email:{
type:String,
require:true
},
password:{
type:String,
require:true
},
rooms:{
type:Array
}
})
module.exports = mongoose.model("User",userSchema);
correct syntax for updating the record.
Make changes in your controller..
module.exports.createroom = async (req,res) => {
const {roomid,email} = req.body;
const user = await UserModel.findOneAndUpdate(
{ email : email },
{ $push: { rooms: roomid } },
)
res.send("done")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With