英文:
How to auto-increment id field of mongodb with golang mgo driver?
问题
我是你的中文翻译助手,以下是翻译好的内容:
我对MongoDB和Golang都不太熟悉。在我的一个项目中,我想要用Go连接MongoDB。我正在使用mgo.v2驱动程序来连接MongoDB和Go。我的问题是:
如何自动递增文档的_id字段,以便每当我尝试执行POST操作时,它都会自动递增文档的_id字段?
我想要实现类似于"FindAndModify"函数的功能,但是我在Go中找不到这个函数。
以下是我在Go中想要尝试的代码。
type count struct {
ID string `bson:"_id"`
Seq int `bson:"seq"`
}
var doc count
func getNextSequence(name string) int {
change := mgo.Change{
Update: collection.Update(count{ID: "userid"}, bson.M{"$inc": count{Seq: 1}}),
ReturnNew: true,
}
_, err1 := collection.Find(bson.M{}).Apply(change, &doc)
return doc.Seq
}
func main() {
fmt.Println(getNextSequence("userid"))
fmt.Println(getNextSequence("userid"))
doc2 := msg{ID: getNextSequence("userid"), Name: "Sarah"}
doc3 := msg{ID: getNextSequence("userid"), Name: "Sarah2"}
}
我尝试了上面的代码,但是Seq的值似乎没有递增。每次调用该函数时,它都会返回0。
感谢您的帮助。
英文:
I am new to mongodb and golang. In one of my project i want to connect mongo with go. I am using mgo.v2 driver for connecting mongo with go. My question is:
How can i auto-increment the _id field of my document so that whenever i try to perform POST operation, it should auto increment the _id field of the document ?
I want to implement something similar to "FindAndModify" function but i don't see this function in go.
This is what i want to try in go.
Auto increment id in mongodb
type count struct {
ID string `bson:"_id"`
Seq int `bson:"seq"`
}
var doc count
func get NextSequence(name string) int{
change := mgo.Change{
Update: collection.Update(count{ID: "userid"}, bson.M{"$inc": count{Seq: 1}}),
ReturnNew: true,
}
_, err1 := collection.Find(bson.M{}).Apply(change, &doc)
return doc.Seq
}
func main(){
fmt.Println(getNextSequence("userid"))
fmt.Println(getNextSequence("userid"))
doc2 := msg{ID: getNextSequence("userid"), Name: "Sarah"}
doc3 := msg{ID: getNextSequence("userid"), Name: "Sarah2"}
}
I tried the above code, but the value of Seq does not seem to increment.It gives me 0 everytime i make a call to the function.
Thanks for the help.
答案1
得分: 4
根据mgo包的文档,你可以使用Query.Apply来实现这个功能。我自己没有尝试过,但在文档中给出的示例似乎已经实现了你想要的效果:
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"n": 1}},
ReturnNew: true,
}
info, err = col.Find(M{"_id": id}).Apply(change, &doc)
fmt.Println(doc.N)
英文:
According to the mgo package documentation, you can use Query.Apply for that. I haven't tried it myself, but the example given there seems to already do what you want to achieve:
change := mgo.Change{
Update: bson.M{"$inc": bson.M{"n": 1}},
ReturnNew: true,
}
info, err = col.Find(M{"_id": id}).Apply(change, &doc)
fmt.Println(doc.N)
答案2
得分: 1
尝试以下方法:
声明一个名为 total_doc
的额外变量,类型为 int
。
然后执行 collection.Find(bson.M{}).Count(total_doc)
,这将返回你在 MongoDB 数据库中的文档总数。
在添加新文档时,使用 doc.Seq = total_doc + 1
。这将在每添加一个新文档时递增序列值。
希望这对你有帮助。
英文:
Try the below one
Declare one more variable name var total_doc int'
Then do collection.Find(bson.M{}).Count(total_doc)
this will return the total number of document your have in you mongodb database.
After add doc.Seq = total_doc + 1
.This will keep on incrementing the sequence value whenever a new document gets added.
Hope this could help you
答案3
得分: 1
可能有点晚,但你也可以使用night-codes/mgo-ai包来管理你的序列。
我个人使用了这个包,我创建了一个单独的sequences
集合来存储递增的值。我还保留了_id
字段,并为我的sequence
值设置了一个单独的id
字段。
从它的README.md
中可以看到:
package main
import (
"github.com/night-codes/mgo-ai"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
// 连接 AutoIncrement 到集合 "counters"
ai.Connect(session.DB("example-db").C("counters"))
// ...
// 使用 AutoIncrement
session.DB("example-db").C("users").Insert(bson.M{
"_id": ai.Next("users"),
"login": "test",
"age": 32,
}
}
希望对你有帮助!
英文:
Might be late but you can also use night-codes/mgo-ai package to manage your sequences.
I personally used this, I created a separate sequences
collection to store the incremented values. I also kept the _id
field and had a separate id
field for my sequence
value.
From its README.md
:
package main
import (
"github.com/night-codes/mgo-ai"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
// connect AutoIncrement to collection "counters"
ai.Connect(session.DB("example-db").C("counters"))
// ...
// use AutoIncrement
session.DB("example-db").C("users").Insert(bson.M{
"_id": ai.Next("users"),
"login": "test",
"age": 32,
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论