1

I'm newbie for manipulating MongoDB recently, but I really need to update some fields like these..

db_name: test   
table_name: info

{
    "_id" : ObjectId("54bf9ab4f8eda6747567b122"),
    "archives" : [
        {
            "evalution" : {
                "positive" : 0,
                "undefine" : 0,
                "negative" : 0
            },
            "source" : ObjectId("54cb6f455decd8037528756b")
        }
    ]
}

I want to increase positive and undefine by 1, and if evalution doesn't exists,

"evalution" : {
    "positive" : 0,
    "undefine" : 0,
    "negative" : 0
} 

should be added to the object.

I don't know if I express myself clearly, but I really need some help..

6
  • Do you want to update all the elements in the archives array? Commented Feb 5, 2015 at 3:10
  • @BatScream I just want to update positive and undefine in evalution Commented Feb 5, 2015 at 3:25
  • Thanks for the clarification. But archives array may have multiple elements, each element may have a evaluation attribute. So you want to update positive and undefine of all the elements? Commented Feb 5, 2015 at 3:52
  • @BatScream so sorry, I didn't explain clearly, I just want to update this one only --> "_id" : ObjectId("54bf9ab4f8eda6747567b122") Commented Feb 5, 2015 at 4:02
  • 1
    is the source filed exists for each sub document? Commented Feb 5, 2015 at 4:08

2 Answers 2

3

If evalution doesn't exists u can create a sub-doc using following query.

db.test.update(
 {_id:ObjectId("54bf9ab4f8eda6747567b122"),
 "archives.source" : ObjectId("54cb6f455decd8037528756b"),
 "archives.evalution":{$exists:false}},
 {$set:{"archives.$.evalution":{positive:1,negative:0,undefine:1}}}
)

NOTE: Positional operator is only upto one level, so u cannot increment the values until you know the index of archieves array.

you get the the index of the archieves array and increment using following command. In this case 0 where source = ObjectId("54cb6f455decd8037528756b")

db.test.update(
 {_id:_id:ObjectId("54bf9ab4f8eda6747567b122")},
 {$inc:{"archives.0.evalution.positive":1}}
)
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe I can't really understand your meaning, may be what the wrong way I did seemed not to be succeed --> db.test.update({'_id':ObjectId('54bf9ab4f8eda3857567b122')}, {$set:{'archives.$.evalution':{'positive':1}}})
but I want to $inc positive and undefine instead of use $set, so sorry I can't really understand your meaning ..
1

It would help to know what the semantics of the update are. Why are you incrementing those two fields? Why are you incrementing two fields, as if doing a count, but if the fields don't exist you want to set default values to 0 (as opposed to, say, 1)? Is there a semantic difference between "evaluation doesn't exist" and the value

{
    "positive" : 0,
    "undefine" : 0,
    "negative" : 0
}

for evaluation? Or between "evaluation doesn't exist" and

{
    "positive" : -1,
    "undefine" : -1,
    "negative" : 0
}

for the value? It seems like no, for at least one of those default values, and so you should set evaluation to have the appropriate default value on insert of the document or of the array element. If you do that, your update is simple:

db.info.update(
    { 
        "_id" : ObjectId("54bf9ab4f8eda6747567b122"), 
        "archives.source" : ObjectId("54cb6f455decd8037528756b")
    },
    {
        "$inc" : { 
            "archives.$.evalution.positive" : 1,
            "archives.$.evalution.undefine" : 1,
        }
    }
)

Also, you have some typos, I think:

  1. evalution probably means evaluation or possibly evolution?
  2. the proper complement to positive and negative is undefined

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.