2014년 12월 7일 일요일

update increment subfiled count

{
    "Name" : "nameof person",
    "_id" : ObjectId("54461c480d9f041a21b4bba9"),
    "items" : [
        {
            "name" : "Item1",
            "count" : 1
        },
        {
            "name" : "Item2",
            "count" : 2
        },
        {
            "name" : "Item3",

            "count" : 1
        }
    ],
    "Town" : 29,
    "Date_Time" : 20141109114512
}

{
    "Name" : "nameof person",
    "_id" : ObjectId("
54461c480d9f041a21b413460"),
    "items" : [
        {

}
            "name" : "Item1",
            "count" : 2
        }

Incrementing the 1st record ........Nothing happens, it does not increment the count of Item1 to 2
 db.item_table.update({"_id" : ObjectId(
"54461c480d9f041a21b4bba9"), 'items.name': 'Item1','Name':"name of person"}, {'$inc': {'items.$.count': 1}})

but for the second record which contains only Item1, it  increments the count to 3

 db.item_table.update({"_id" : ObjectId("54461c480d9f041a21b413460"), 'items.name': 'Item1','Name':"name of person"}, {'$inc': {'items.$.count': 1}}) 
{
    "Name" : "nameof person",
    "_id" : ObjectId("
54461c480d9f041a21b413460"),
    "items" : [
        {

}
            "name" : "Item1",
            "count" : 3


}

I  tried update with "multi:true" and also "
'items.name':{"$in":['Item1']" (in condition)  before posting, but nothing works. kindly help

-KR
        }



It's a typo in your update: "nameof person" in the document vs. "name of person" in the update. Note that that condition on Name is redundant given the match on _id.



Yeah true, 'nameof person' was a typo and anyway as you mentioned it is redundant, it was one of the possible combination I was trying, but I am not getting, how to update single item in a sub document,   



If some one stuck with the same problem , here is the solution

http://www.wiredprairie.us/blog/index.php/archives/1895


댓글 없음:

댓글 쓰기