利用mongo的findAndModify子性操作實現auto increment ID

NO IMAGE
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

實際情況

預設情況下,mongo使用_id

輸出:

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:tel 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 main.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:28 main.go:54: insert: map[_id:202 username:name 5 telephone:tel  51] 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 username: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 username:name 6 telephone:tel 320] 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/28 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] success!

mongo查詢結果

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" }

相關文章

伺服器 最新文章