0

HI All I have this Data in mongo,

{"articleId" : [
            {
                    "articleId" : "9514666",
                    "articleCount" : 1
            }
    ],

    "count" : NumberLong(1),
    "timeStamp" : NumberLong("1416634200000"),
    "interval" : 1,
    "tags" : "famous"
 }

I want to update it using this new data

 {"articleId" : [      
            {
                    "articleId" : "9514666",
                    "articleCount" : 3
            }
            {
                    "articleId" : "9514667",
                    "articleCount" : 3
            }
    ],

    "count" : NumberLong(6),
    "timeStamp" : NumberLong("1416634200000"),
    "interval" : 1,
    "tags" : "famous"
 }

What i need in the output is

{"articleId" : [
            {
                    "articleId" : "9514666",
                    "articleCount" : 4
            }
            {
                    "articleId" : "9514667",
                    "articleCount" : 3
            }
    ],

    "count" : NumberLong(7),
    "timeStamp" : NumberLong("1416634200000"),
    "interval" : 1,
    "tags" : "famous"
 }

Could you please suggest me how can i achieve this this using update operation My update query will have tags field as query parameter.

2
  • I would suggest posting this to stackoverflow. It is a better forum for development questions. Dec 11, 2014 at 13:35
  • @JamesWahlin i posted it here as i thought it could be done by some mongo operation
    – viren
    Dec 16, 2014 at 5:19

1 Answer 1

1

I accomplished it with your criteria which was to use the tags field in your query. I of course had to filter on the document further to only update that specific document. I presumed other documents may have a tag of "famous". I created two documents with tags:famous to prove this worked when updating 1 document. Here's my solution with comments.

//basics 
db = test
collection = test

//data looked like this with two documents for testing
/* 0 */
{
    "_id" : ObjectId("5498778567bbbc2fff723515"),
    "articleId" : [ 
        {
            "articleId" : "9514666",
            "articleCount" : 1
        }
    ],
    "count" : 1,
    "timeStamp" : "1416634200000",
    "interval" : 1,
    "tags" : "famous"
}

/* 1 */
{
"_id" : ObjectId("5498781567bbbc2fff723516"),
"articleId" : [ 
    {
        "articleId" : "951466622",
        "articleCount" : 22
    }
],
"count" : 1,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}


//confirmed I can query the one document like this
db.test.find({'tags': 'famous','articleId.articleId':'9514666'})

//was able to execute an update as described
db.test.update({'tags': 'famous','articleId.articleId':'9514666'}, {"articleId" : [
        {
                "articleId" : "9514666",
                "articleCount" : 4
        },
        {
                "articleId" : "9514667",
                "articleCount" : 3
        }
],

"count" : 7,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}) 


//Resulting update to document is
/* 0 */
{
 "_id" : ObjectId("5498778567bbbc2fff723515"),
 "articleId" : [ 
    {
        "articleId" : "9514666",
        "articleCount" : 4
    }, 
    {
        "articleId" : "9514667",
        "articleCount" : 3
    }
],
"count" : 7,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}

//showing both documents, only first one updated obviously
/* 0 */
{
"_id" : ObjectId("5498778567bbbc2fff723515"),
"articleId" : [ 
    {
        "articleId" : "9514666",
        "articleCount" : 4
    }, 
    {
        "articleId" : "9514667",
        "articleCount" : 3
    }
],
"count" : 7,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}

/* 1 */
{
"_id" : ObjectId("5498781567bbbc2fff723516"),
"articleId" : [ 
    {
        "articleId" : "951466622",
        "articleCount" : 22
    }
],
"count" : 1,
"timeStamp" : "1416634200000",
"interval" : 1,
"tags" : "famous"
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.