Home > Back-end >  user updates using Mongoose mongodb push not working
user updates using Mongoose mongodb push not working

Time:01-11

I am trying to use MongoDB Mongoose MongoDB push to update an entity such that the dcrLocations array gets the dcrlocation entity pushed into it

This is my response from the MongoDB server

{
  acknowledged: true,
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 0
}

Heres my model code import mongoose from "mongoose";

//User Schema
const UserSchema = new mongoose.Schema({
            firstName: {
                type: String,
                required: true,
            },
            lastName: {
                type: String,
                required: true,
            },
            username: {
                type: String,
            },
            email: {
                type: String,
                required: true,
                lowercase: true,
                trim: true,
                unique: true,
                index: true,
            },
            password: {
                type: String,
                required: true,
                trim: true,
            },
            hasSubscription: {
                type: Boolean,
                default: false
            },
        dcrLocations: [
                    {
                        dcrLocationId: {
                            type: mongoose.Schema.Types.ObjectId,
                            ref: 'DCRLocation'
                        }
                    },
                ]
        },
        {
            toJSON: {
                transform(doc, ret) {
                    delete ret.password,
                        delete ret.__v,
                        delete ret.createdAt,
                        delete ret.updatedAt
                }
            }
            ,
            timestamps: true,

        }
    )
;

UserSchema.index({email: 1, phone: 1})

export default mongoose.model("User", UserSchema);

Heres my controller code

 import {User} from "../../path/to/model.js";
import {ObjectID as ObjectId} from "mongodb";

    
    function functionName() {
        return {
            AssDCRLoToUsr: async (req, res) => {
                try {
                    const {dcrLocationId} = req.body;
                    console.log(dcrLocationId)
    
                    const userToUpdate = await User.updateOne(
                        {_id: req.params.id},
                        {
                            $push: {
                                dcrLocations: {
                                    dcrLocationId: new ObjectId(dcrLocationId)
                                }
                            }
                        }
                    );
                    console.log(userToUpdate)
                    res.json(userToUpdate)
                } catch (error) {
                    console.error(error)
                    res.json(false)
                }
            },
    
    
        }
    }
    
    export {functionName};

Any advice on how I can make this work? I have been battling with it for some time now

CodePudding user response:

try this

$addToSet is used to add unique element in array

import {User} from "../../path/to/model.js";
import {ObjectID as ObjectId} from "mongodb";

    
    function functionName() {
        return {
            AssDCRLoToUsr: async (req, res) => {
                try {
                    const {dcrLocationId} = req.body;
                    console.log(dcrLocationId)
    
                    const userToUpdate = await User.updateOne(
                        {_id: req.params.id},
                        {
                            $addToSet: {
                                dcrLocations: {
                                    dcrLocationId: new ObjectId(dcrLocationId)
                                }
                            }
                        }
                    );
                    console.log(userToUpdate)
                    res.json(userToUpdate)
                } catch (error) {
                    console.error(error)
                    res.json(false)
                }
            },
    
    
        }
    }
    
    export {functionName};

  • Related