這裏是工作正常,我(和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() {
// 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)
}
片斷的語法看起來是正確的。你能展示更多的代碼嗎? – 2014-12-05 20:09:24