2

my collection is as follows :

   "_id" : ObjectId("5751f7892ae95d601f40411d"),

   "doc" : [
    {
        "org" : ObjectId("5751f7892ae95d601f40411c"),
        "action" : 0,
        "_id" : ObjectId("5751f7892ae95d601f40411e")
    },
    {
        "org" : ObjectId("5751952cace204c507fad255"),
        "action" : 1,
        "_id" : ObjectId("575217ce341cf6512b8dff39")
    }     ]

I want to update action field in the doc with org:5751952cace204c507fad255 so action will equal 2 I know this has already been answered many times but it's not working for me

Here is what I tried but Collection didn't change:

     Model.update(
    {
        "_id":ObjectId("5751f7892ae95d601f40411d"),
        "doc.org":ObjectId("5751952cace204c507fad255")
    },
    {
        "$inc": {
                "doc.$.action": 1
        }
    }
)
Youssef Korchi
  • 127
  • 1
  • 4
  • 14
  • Where are you getting the `ObjectId()` wrapper from? – chridam Jun 06 '16 at 14:10
  • var ObjectId = require('mongoose').Types.ObjectId; I also thought it's a problem finding my collection but I tried Model.find with the same conditions and printed the result and my collection was successfully found – Youssef Korchi Jun 06 '16 at 14:18
  • Have you tried updating without wrapping the string `_id`s? – chridam Jun 06 '16 at 14:33
  • Yes I tried both. I think my problem is not about the nested document , because I have just tried to update the name of my collection since it has a field name : db.Employee.update({_id:ObjectId("575194e6ace204c507fad250")},{FName:"youssef"}) , It says WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) , but when I go to see my Employee collection FName is not changed – Youssef Korchi Jun 06 '16 at 14:40

1 Answers1

1

can try using positional operator $ to increment matched action doc.$.action

like:

//assume you passed modelId and orgId in request body
// According to your tag you may used mongoose so use mongoose.Types.ObjectId('5751f7892ae95d601f40411d') instead of ObjectId("5751f7892ae95d601f40411d")
// or direct req.body.modelId without convert
Model.update(
    {
        "_id": req.body.modelId,
        "doc.org": req.body.orgId
    },
    {
        "$inc": {
                "doc.$.action": 1
        }
    },
    function(error, updatedData) {
        if(error) {
            return res.status(400).send(error);
        }
        return res.status(200).send(updatedData);
    }
);
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
  • Sorry I just forgot to put the $ when I was writing my question , actually even with doc.$.action, my collection didn't update – Youssef Korchi Jun 06 '16 at 14:03
  • you can try using `"_id": req.body.modelId` or `mongoose.Types.ObjectId("25...")` for both `_id` and `doc.org` – Shaishab Roy Jun 06 '16 at 14:18
  • I tried both but to no avail, I also tried in Mongo shell , when I add this to my query { upsert: true } , I get error : "errmsg" : "The positional operator did not find the match needed from the query. Unexpanded update: doc.$.action" – Youssef Korchi Jun 06 '16 at 14:29
  • that means no document found according to your match condition. – Shaishab Roy Jun 06 '16 at 14:37
  • if you want to update specific one document use `Model.findOneAndUpdate()` – Shaishab Roy Jun 06 '16 at 14:41
  • I tried something simple by trying to change a field named : "FName" : b.Employee.update({_id:ObjectId("575194e6ace204c507fad250")},{FName:"newFname"}) , It says WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) , but when I go to see my Employee collection FName is not changed – Youssef Korchi Jun 06 '16 at 14:42
  • you may also get idea why you got this error message. from this link answer http://stackoverflow.com/questions/23470658/mongodb-upsert-sub-document – Shaishab Roy Jun 06 '16 at 14:44
  • should use `{$set: {FName:"newFname"}},{new: true}` where `new: true` will return updated document – Shaishab Roy Jun 06 '16 at 14:55
  • db.Employee.update({_id:ObjectId("575194e6ace204c507fad250")},{$set:{FName:"youssef"}},{new: true}) -> WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) it's mached but not modified – Youssef Korchi Jun 06 '16 at 15:03
  • Shaishab Roy , this is getting really weird , now I tried FindOneAndUpdate like you said before, and it returned my update document { "_id" : ObjectId("575194e6ace204c507fad250"), "FName" : "newName", "LName" : "youssef" } but when I do Employee.find to make sure it's modified , it still got the old FName value – Youssef Korchi Jun 06 '16 at 15:13
  • can you show me your server side function where you implemented this also model? – Shaishab Roy Jun 07 '16 at 06:45
  • var org_permissionSchema = new mongoose.Schema( { org: {type: mongoose.Schema.ObjectId, ref: 'Organization'}, action:Number } ); var Employee= module.exports=mongoose.model('Employee',{ doc:[org_permissionSchema] }); | my updating code : Employee.update( {"_id":req.body.emp_id,"doc.org":req.body.org}, {$set:{"doc.$.action":55}} ) – Youssef Korchi Jun 07 '16 at 14:32
  • Hi shaishab roy , this worked for me in mongo shell : db.employees.update({"_id":ObjectId("5756d0bcb9c2c7ae1c0e6e0f"),"doc.org":ObjectId("5756d0d2b9c2c7ae1c0e6e11")},{$set:{"doc.$.action":140}}) I tried the same code but it's not working on the server side with mongoose , – Youssef Korchi Jun 07 '16 at 14:47
  • Finally it's working , I just had to add a callback function in the Employee.update(......,function(err,raw){}) , apparently this function was necessary to save my update even if I'm not using it – Youssef Korchi Jun 07 '16 at 15:15