如何在mgo中构建一个$or查询

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

How to construct an $or query in mgo

问题

我正在尝试将这个JS MongoDB查询转换为Go mgo查询:

  1. var foo = "bar";
  2. db.collection.find({"$or": [{uuid: foo}, {name: foo}]});

这是我目前的代码,但它不起作用:

  1. conditions := bson.M{"$or": []bson.M{bson.M{"uuid": name}, bson.M{"name": name}}}

编辑:现在似乎可以工作了。也许我在某个地方打错了字。

英文:

I am trying to convert this JS MongoDB query into Go mgo query:

  1. var foo = "bar";
  2. db.collection.find({"$or": [ {uuid: foo}, {name: foo} ] });

This is what I've got so far, but it doesn't work:

  1. conditions := bson.M{"$or": []bson.M{bson.M{"uuid": name}, bson.M{"name": name}}}

EDIT: It does seem to work now. Maybe I had a typo somewhere.

答案1

得分: 26

这是一个完整的示例,对我来说运行良好(使用Go 1.4和MongoDB 2.6.5)。

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/mgo.v2"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. type Person struct {
  9. Num int
  10. Uuid string
  11. Name string
  12. }
  13. func main() {
  14. // 连接到数据库
  15. session, err := mgo.Dial("localhost")
  16. if err != nil {
  17. panic(err)
  18. }
  19. defer session.Close()
  20. // 如果存在,则删除people集合
  21. c := session.DB("test").C("people")
  22. c.DropCollection()
  23. // 添加一些数据
  24. err = c.Insert(&Person{1, "UUID1", "Joe"},
  25. &Person{2, "UUID2", "Jane"},
  26. &Person{3, "UUID3", "Didier"})
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. result := Person{}
  31. err = c.Find(bson.M{"$or": []bson.M{bson.M{"uuid": "UUID0"}, bson.M{"name": "Joe"}}}).One(&result)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. fmt.Println(result)
  36. }

希望对你有帮助!

英文:

Here is a complete example which works fine for me (with Go 1.4, and MongoDB 2.6.5)

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "gopkg.in/mgo.v2"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. type Person struct {
  9. Num int
  10. Uuid string
  11. Name string
  12. }
  13. func main() {
  14. // Connect to the database
  15. session, err := mgo.Dial("localhost")
  16. if err != nil {
  17. panic(err)
  18. }
  19. defer session.Close()
  20. // Remove people collection if any
  21. c := session.DB("test").C("people")
  22. c.DropCollection()
  23. // Add some data
  24. err = c.Insert(&Person{ 1, "UUID1", "Joe"},
  25. &Person{ 2, "UUID2", "Jane"},
  26. &Person{ 3, "UUID3", "Didier" })
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. result := Person{}
  31. err = c.Find( bson.M{ "$or": []bson.M{ bson.M{"uuid":"UUID0"}, bson.M{"name": "Joe"} } } ).One(&result)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. fmt.Println(result)
  36. }

答案2

得分: 0

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. "gopkg.in/mgo.v2/bson"
  6. )
  7. func GetAll() []interface{} {
  8. // 数据库实例
  9. session, err := mgo.Dial("localhost")
  10. c := session.DB("yourDbName").C("YourCollectionName")
  11. foo := "bar"
  12. orQuery := []bson.M{}
  13. uidFindQuery := bson.M{"uuid": foo}
  14. nameFindQuery := bson.M{"name": foo}
  15. orQuery = append(orQuery, uidFindQuery, nameFindQuery)
  16. result := []interface{}{}
  17. err = c.Find(bson.M{"$or": orQuery}).All(&result)
  18. fmt.Println("错误", err)
  19. fmt.Println("您期望的数据", result)
  20. return result
  21. }

参考链接:
https://gopkg.in/mgo.v2 Go语言中最好的MongoDB接口

英文:
  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/mgo.v2"
  5. "gopkg.in/mgo.v2/bson"
  6. )
  7. func GetAll()[]interface{}{
  8. //Data Base Instance
  9. session, err := mgo.Dial("localhost")
  10. c := session.DB("yourDbName").C("YourCollectionName")
  11. foo := "bar"
  12. orQuery := []bson.M{}
  13. uidFindQuery := bson.M{uuid: foo}
  14. nameFindQuery := bson.M{name: foo}
  15. orQuery = append(orQuery, uidFindQuery, nameFindQuery)
  16. result := []interface{}{}
  17. err := c.Find(bson.M{"$or":orQuery}).All(&result);
  18. fmt.Println("error", err)
  19. fmt.Println("Your expected Data",result)
  20. return result
  21. } `

>Reference
https://gopkg.in/mgo.v2 Best Mongo db interface for go lang

huangapple
  • 本文由 发表于 2014年12月6日 03:39:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/27323408.html
匿名

发表评论

匿名网友

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

确定