使用共享会话变量从我的函数中插入 MongoDB 数据。

huangapple go评论104阅读模式
英文:

mongodb insert from my function using shared session variables

问题

嗨,我已经成功翻译了你的内容,以下是翻译结果:

嗨,我成功地使以下文档插入工作,并且可以调用该函数。然而,我怀疑该函数中的数据库连接部分效率不高,因为我每分钟调用它30-40次。

我需要将数据库会话连接移到函数外部,我认为与*mongoSession有关,但无法使其正常工作。希望能得到帮助。

简而言之:如何将连接移到函数外部

  1. func insertmgo(aaa string, bbb string, ccc time.Time, wg *sync.WaitGroup) {
  2. // mongo stuff
  3. mongoDBDialInfo := &mgo.DialInfo{
  4. Addrs: []string{MongoDBHosts},
  5. Timeout: 60 * time.Second,
  6. Database: AuthDatabase,
  7. Username: AuthUserName,
  8. Password: AuthPassword,
  9. }
  10. mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
  11. if err != nil {
  12. log.Fatalf("CreateSession: %s\n", err)
  13. }
  14. mongoSession.SetMode(mgo.Monotonic, true)
  15. c := mongoSession.DB("x").C("ships")
  16. oneship.Created = ccc
  17. oneship.Name = bbb
  18. oneship.Type = aaa
  19. c.Insert(oneship)
  20. wg.Done()
  21. }

希望对你有所帮助!

英文:

Hi i got the following document insert to work properly and i can call the function. However, i doubt that the DB connect stuff in this function is very efficient since i call it 30-40 times per minute.

i need to move the db session connect outside my function and i think its something with *mongoSession, but cant get it to work. Any help would be appriciated.

tldr: How do i move the connection outside the function

  1. func insertmgo(aaa string, bbb string, ccc time.Time, wg *sync.WaitGroup) {
  2. // mongo stuff
  3. mongoDBDialInfo := &mgo.DialInfo{
  4. Addrs: []string{MongoDBHosts},
  5. Timeout: 60 * time.Second,
  6. Database: AuthDatabase,
  7. Username: AuthUserName,
  8. Password: AuthPassword,
  9. }
  10. mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
  11. if err != nil {
  12. log.Fatalf("CreateSession: %s\n", err)
  13. }
  14. mongoSession.SetMode(mgo.Monotonic, true)
  15. c := mongoSession.DB("x").C("ships")
  16. oneship.Created = ccc
  17. oneship.Name = bbb
  18. oneship.Type = aaa
  19. c.Insert(oneship)
  20. wg.Done()
  21. }

答案1

得分: 1

找到解决方案

  1. 主函数 {
  2. MongoDB操作
  3. mongoDBDialInfo := &mgo.DialInfo{
  4. Addrs: []string{MongoDBHosts},
  5. Timeout: 60 * time.Second,
  6. Database: AuthDatabase,
  7. Username: AuthUserName,
  8. Password: AuthPassword,
  9. }
  10. mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
  11. if err != nil {
  12. log.Fatalf("CreateSession: %s\n", err)
  13. }
  14. }

使用以下方式调用函数

  1. fun(mongoSession, .....)

在你的函数中

  1. func fun(db *mgo.Session, .... , wg *sync.WaitGroup) {

我认为就是这样了。不确定是否还有其他或更好的方法,但这对我来说似乎有效。

英文:

found soloution

  1. main {
  2. mongo stuff
  3. mongoDBDialInfo := &mgo.DialInfo{
  4. Addrs: []string{MongoDBHosts},
  5. Timeout: 60 * time.Second,
  6. Database: AuthDatabase,
  7. Username: AuthUserName,
  8. Password: AuthPassword,
  9. }
  10. mongoSession, err := mgo.DialWithInfo(mongoDBDialInfo)
  11. if err != nil {
  12. log.Fatalf("CreateSession: %s\n", err)
  13. }
  14. }

call function with

  1. fun(mongoSession, .....)

in your function

  1. func fun(db *mgo.Session, .... , wg *sync.WaitGroup) {

that was is i think. not sure if there is other or better ways to do it, but this seem to work for me.

huangapple
  • 本文由 发表于 2014年9月22日 15:28:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/25968701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定