Implement auto increment ID with Findandmodify atomic operation of MONGO

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Actual situation

By default, MONGO uses _id auto-generated Uniq IDs, and in MONGO commands, you cannot specify a self-increment field. The self-increment field must be atomic in multi-threading, which is difficult to achieve in large data scenarios (scalability).

Generally in MongoDB, you would is not a auto-increment pattern for
The _id field, or any field, because it does not a scale for databases
With large numbers of documents. Typically the default value ObjectId
is more ideal for the _id.

The good news, though, is that _id it doesn't have to be a objectid type, it can be any type. Moreover, in the MONGO, there is an findandmodify command that is atomic in nature. We can use it to implement the auto increment ID.
In Golang's MgO library, Apply commands can be used to implement findAndModify functions

Specific policies and codes

Strategy

Suppose we want to set the field of the test table _id to be self-increment, we need to create a counter table that holds a seq field, and, by seq using apply, gets the new doc's SEQ value as the value of the next ID in the test table.

The code is as follows:

Package Mainimport ("FMT" "Gopkg.in/mgo.v2" "Gopkg.in/mgo.v2/bson" "Log" "Math/rand" "Sync") func init () {log. SetFlags (log. Lshortfile | Log. Lstdflags)}func Main () {session, Err: = MgO. Dial ("Usr:pwd@127.0.0.1:27017/dbname") if err! = Nil {log. Fatal ("Unable to open mongodb! ") return} defer session. Close () CLT: = Session. DB ("dbname"). C ("Test") Getnextseq: = Func () func () int {counter: = session. DB ("dbname"). C ("Counter") cid: = "Counterid" return func () int {change: = MgO. change{Update:bson. m{"$inc": Bson. m{"seq": 1}, Upsert:true, Returnnew:true,} doc: = struct{seq I NT} {} If _, Err: = counter. Find (Bson. m{"_id": CID}). Apply (change, &doc); Err! = Nil {panic (FMT. Errorf ("Get Counter failed:", err.) Error ()))} log. Println ("seq:", doc) return doc. SEQ}} () WG: =Sync. waitgroup{}//Create 10 Go routine simulated multithreaded environment for I: = 0; I < 10; i++ {WG.                ADD (1) go func (i int) {_row: = map[string]interface{}{"_id": Getnextseq (), "username": FMT. Sprintf ("name%2d", I), "telephone": FMT. Sprintf ("Tel%4d", Rand. Int63n ()} If err: = CLT. Insert (_row); Err! = Nil {log. Printf ("Insert%v failed:%v\n", _row, Err)} else {log. Printf ("Insert:%v success!\n", _row)} WG. Done ()} (i)} WG. Wait ()}

Output:

2016/04/28 17:05:28 main.go:37:seq: {201}2016/04/28 17:05:28 main.go:54:insert:map[_id:201 username:name 9 telephone:t El 410] Success!2016/04/28 17:05:28 main.go:37:seq: {204}2016/04/28 17:05:28 main.go:37:seq: {203}2016/04/28 17:05:28 ma In.go:37:seq: {202}2016/04/28 17:05:28 main.go:37:seq: {206}2016/04/28 17:05:28 main.go:37:seq: {205}2016/04/28 17:05: main.go:54:insert:map[_id:202 username:name 5 Telephone:tel] Success!2016/04/28 17:05:28 main.go:54:insert:map[ _id:204 Username:name 2 Telephone:tel 551] Success!2016/04/28 17:05:28 Main.go:54:insert:map[telephone:tel 821 _id:203 u Sername:name 1] Success!2016/04/28 17:05:28 main.go:37:seq: {207}2016/04/28 17:05:28 main.go:54:insert:map[_id:205 use  Rname:name 6 Telephone:tel] Success!2016/04/28 17:05:28 main.go:54:insert:map[_id:206 username:name 4 Telephone:tel 937] SUCCESS!2016/04/28 17:05:28 main.go:54:insert:map[_id:207 username:name 3 Telephone:tel 758] SUCCESS!2016/04/28 17 : 05:28 main.go:37:seq: {208}2016/04/28 17:05:28 main.go:54:insert:map[username:name 7 Telephone:tel 148 _id:208] Success!2016/04/28 17:05:28 Main. Go:37:seq: {209}2016/04/28 17:05:28 main.go:54:insert:map[_id:209 username:name 0 Telephone:tel 216] Success!2016/04/2 8 17:05:28 main.go:37:seq: {210}2016/04/28 17:05:28 main.go:54:insert:map[_id:210 username:name 8 telephone:tel 449] Su ccess!

MONGO Query Results

db.test.find({_id:{$gt:200}}){ "_id" : 201, "username" : "name 9", "telephone" : "tel 410" }{ "_id" : 202, "username" : "name 5", "telephone" : "tel  51" }{ "_id" : 203, "username" : "name 1", "telephone" : "tel 821" }{ "_id" : 204, "username" : "name 2", "telephone" : "tel 551" }{ "_id" : 205, "username" : "name 6", "telephone" : "tel 320" }{ "_id" : 206, "username" : "name 4", "telephone" : "tel 937" }{ "_id" : 207, "username" : "name 3", "telephone" : "tel 758" }{ "_id" : 208, "username" : "name 7", "telephone" : "tel 148" }{ "_id" : 209, "username" : "name 0", "telephone" : "tel 216" }{ "_id" : 210, "username" : "name 8", "telephone" : "tel 449" }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.