Upsert and Increment in one transaction

2,224 views
Skip to first unread message

mac...@gmail.com

unread,
Apr 27, 2013, 4:03:10 AM4/27/13
to mgo-...@googlegroups.com
I'm trying to Upsert a document and if it exists, increment the count by 1.  I can do it in Mongo, but I don't have any idea how to nest the $inc correctly in mgo.  The documentation doesn't specify and I wasn't able to find any other posts about it. Any advice?

Mongo Query:
> db.links.remove()
> db.links.find( )
> db.links.update({"link":"www.example.com"}, {$set:{"slink":"g.g/1"}, $inc:{"count": 1}}, {upsert:true} )
> db.links.find( )
{ "_id" : ObjectId("517b854753a468b22f8ac7c6"), "count" : 1, "link" : "www.example.com", "slink" : "g.g/1" }
> db.links.update({"link":"www.example.com"}, {$set:{"slink":"g.g/1"}, $inc:{"count": 1}}, {upsert:true} )
> db.links.update({"link":"www.example.com"}, {$set:{"slink":"g.g/1"}, $inc:{"count": 1}}, {upsert:true} )
> db.links.update({"link":"www.example.com"}, {$set:{"slink":"g.g/1"}, $inc:{"count": 1}}, {upsert:true} )
> db.links.update({"link":"www.example.com"}, {$set:{"slink":"g.g/1"}, $inc:{"count": 1}}, {upsert:true} )
> db.links.find( )
{ "_id" : ObjectId("517b854753a468b22f8ac7c6"), "count" : 5, "link" : "www.example.com", "slink" : "g.g/1" }


Go Query:
change := mgo.Change{
// ???
                //Update: {bson.M{"$set": bson.M{"link": qLink}}, bson.M{"$inc": bson.M{"count": 1}}},
//Update:    bson.M{ bson.M{"$set": bson.M{"link": qLink}}, bson.M{"$inc": bson.M{"count": 1}} },
//Update: bson.M{"link": qLink, "slink": "1234"}, 
Upsert:    true,
Remove:    false,
ReturnNew: true,
}

Gustavo Niemeyer

unread,
Apr 27, 2013, 11:40:25 AM4/27/13
to mgo-...@googlegroups.com
On Sat, Apr 27, 2013 at 5:03 AM, <mac...@gmail.com> wrote:
> I'm trying to Upsert a document and if it exists, increment the count by 1.
> I can do it in Mongo, but I don't have any idea how to nest the $inc
> correctly in mgo. The documentation doesn't specify and I wasn't able to
> find any other posts about it. Any advice?

The document you use with mgo is exactly the same one you use on the shell.

For example:

>> db.links.update({"link":"www.example.com"}, {$set:{"slink":"g.g/1"},
>> $inc:{"count": 1}}, {upsert:true} )

type M map[string]interface{}
c := database.C("links").Upsert(M{"link": "www.example.com"},
M{"$set": M{"slink": g.g/1"}, "$inc": M{"count": 1}})

> Go Query:
> change := mgo.Change{

This is building a request to send a findAndModify command to the
server, which isn't what you're doing in the shell.


gustavo @ http://niemeyer.net

mac...@gmail.com

unread,
Apr 27, 2013, 2:35:51 PM4/27/13
to mgo-...@googlegroups.com
Thank you so much for the assistance, Gustavo.  Do you know of any resources that show more examples like this?

Gustavo Niemeyer

unread,
Apr 27, 2013, 2:47:06 PM4/27/13
to mgo-...@googlegroups.com
On Sat, Apr 27, 2013 at 3:35 PM, <mac...@gmail.com> wrote:
> Thank you so much for the assistance, Gustavo. Do you know of any resources
> that show more examples like this?

My pleasure. As shown, the documents are the same as for plain mongo,
so you can look directly at the mongo documentation and do trivial
conventions such as this when using mgo:

{key: value} => m{key: value}


gustavo @ http://niemeyer.net

vineet daniel

unread,
Feb 5, 2016, 12:42:05 AM2/5/16
to mgo-users
a bit too late...but can help somebody. Here's one example :->
uc.session.DB("<dbname>").C("<collection>").Update(M{"_id": id}, M{"$inc": M{"nooffollowers": 1}})
the above statement searches for a user based upon his id and increments the count by 1
to decrement simply use -1 instead of 1.
Reply all
Reply to author
Forward
0 new messages