0

I have the following Mongoose schema and model:

var deviceSchema = new Schema({
    deviceId:String,
    deviceName:String,
    devicePlace:String,

    socket : [{number: Number,name:String, state : Boolean, current: Number, image:Number,locked:Boolean,reserved:Boolean}]
});

I already have a device in my database with four sockets.

Here example!

This is original data.

{
    "_id" : ObjectId("5626569006bc3da468bafe93"),
    "deviceId" : "0013A20040B5769A",
    "deviceName" : "device",
    "devicePlace" : "place",
    "__v" : 0,
    "socket" : [ 
        {
            "_id" : ObjectId("5628bd83570be84e28879e2d"),
            "number" : 0,
            "name" : "name"
            "state" : true,
            "current" : 0
            "image" : 0,
            "locked" : false,
            "reserved" : false,
        }, ...
    ]
}

and I received data from client for update.

{
    "_id" : ObjectId("5626569006bc3da468bafe93"),
    "deviceId" : "0013A20040B5769A",
    "__v" : 0,
    "socket" : [ 
        {
            "_id" : ObjectId("5628bd83570be84e28879e2d"),
            "number" : 0,
            "name" : "new name!!!!!"
            "state" : true,
            "current" : 0
        }, ... 
    ]
}

Now I'm trying to update a specific socket's name in the database with the following command:

device.update({deviceId: newData.deviceId, "socket.number": newData.number}, {$set: {"socket.$.name": newData.name}})

newData is object that extracted from socket array in received data.

I want to just update first socket's name. or if possible, I want to update every socket's name as received socket array.

But this does not seem to be working, but I get no error. Can someone pin point what I'm doing wrong?

2
  • Code looks fine. What are the newData object values? Can you edit your question to show some sample documents? Commented Oct 22, 2015 at 11:15
  • Thanks, I edited my question!!! Commented Oct 22, 2015 at 11:47

1 Answer 1

1

Add the callback to the update statement to see the error trace.

     device.update({deviceId:newData.deviceId,'socket.number':newData.number}
                  ,{$set: {"socket.$.name" : newData.name}}
                  ,function(error,updatedDevice){

         if(error) throw error;
         // or : console.log("update error",error.message);
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.