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
        }
    }
)
4
  • Where are you getting the ObjectId() wrapper from? Commented Jun 6, 2016 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 Commented Jun 6, 2016 at 14:18
  • Have you tried updating without wrapping the string _ids? Commented Jun 6, 2016 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 Commented Jun 6, 2016 at 14:40

1 Answer 1

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);
    }
);
Sign up to request clarification or add additional context in comments.

14 Comments

Sorry I just forgot to put the $ when I was writing my question , actually even with doc.$.action, my collection didn't update
you can try using "_id": req.body.modelId or mongoose.Types.ObjectId("25...") for both _id and doc.org
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"
that means no document found according to your match condition.
if you want to update specific one document use Model.findOneAndUpdate()
|

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.