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

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

How to construct an $or query in mgo

问题

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

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

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

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:

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

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

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)。

package main

import (
    "fmt"
    "log"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Person struct {
    Num  int
    Uuid string
    Name string
}

func main() {

    // 连接到数据库
    session, err := mgo.Dial("localhost")
    if err != nil {
        panic(err)
    }
    defer session.Close()

    // 如果存在,则删除people集合
    c := session.DB("test").C("people")
    c.DropCollection()

    // 添加一些数据
    err = c.Insert(&Person{1, "UUID1", "Joe"},
        &Person{2, "UUID2", "Jane"},
        &Person{3, "UUID3", "Didier"})
    if err != nil {
        log.Fatal(err)
    }

    result := Person{}
    err = c.Find(bson.M{"$or": []bson.M{bson.M{"uuid": "UUID0"}, bson.M{"name": "Joe"}}}).One(&result)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(result)
}

希望对你有帮助!

英文:

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

package main

import (
    "fmt"
    "log"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Person struct {
    Num int
    Uuid string
    Name string
}

func main() {

    // Connect to the database
    session, err := mgo.Dial("localhost")
    if err != nil {
            panic(err)
    }
    defer session.Close()

    // Remove people collection if any
    c := session.DB("test").C("people")
    c.DropCollection()
    
    // Add some data        
    err = c.Insert(&Person{ 1, "UUID1", "Joe"},
                   &Person{ 2, "UUID2", "Jane"}, 
                   &Person{ 3, "UUID3", "Didier" })
    if err != nil {
            log.Fatal(err)
    }

    result := Person{}
    err = c.Find( bson.M{ "$or": []bson.M{ bson.M{"uuid":"UUID0"}, bson.M{"name": "Joe"} } } ).One(&result)
    if err != nil {
            log.Fatal(err)
    }

    fmt.Println(result)
}

答案2

得分: 0

package main

import (
    "fmt"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

func GetAll() []interface{} {
    // 数据库实例
    session, err := mgo.Dial("localhost")
    c := session.DB("yourDbName").C("YourCollectionName")
    foo := "bar"
    orQuery := []bson.M{}
    uidFindQuery := bson.M{"uuid": foo}
    nameFindQuery := bson.M{"name": foo}
    orQuery = append(orQuery, uidFindQuery, nameFindQuery)
    result := []interface{}{}
    err = c.Find(bson.M{"$or": orQuery}).All(&result)
    fmt.Println("错误", err)
    fmt.Println("您期望的数据", result)
    return result
}

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

英文:
    package main
    
    import (
     "fmt"
        "gopkg.in/mgo.v2"
        "gopkg.in/mgo.v2/bson"
    
    )
func GetAll()[]interface{}{
           //Data Base Instance
        session, err := mgo.Dial("localhost")
        c := session.DB("yourDbName").C("YourCollectionName")
            foo := "bar"
            orQuery := []bson.M{}
            uidFindQuery := bson.M{uuid: foo}
            nameFindQuery := bson.M{name: foo}
            orQuery = append(orQuery, uidFindQuery, nameFindQuery)
             result := []interface{}{}
            err := c.Find(bson.M{"$or":orQuery}).All(&result);
           fmt.Println("error", err)
           fmt.Println("Your expected Data",result)
           return result
            
   } `

>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:

确定