-
-
Save border/3489566 to your computer and use it in GitHub Desktop.
package main | |
import ( | |
"fmt" | |
"labix.org/v2/mgo" | |
"labix.org/v2/mgo/bson" | |
"time" | |
) | |
type Person struct { | |
ID bson.ObjectId `bson:"_id,omitempty"` | |
Name string | |
Phone string | |
Timestamp time.Time | |
} | |
var ( | |
IsDrop = true | |
) | |
func main() { | |
session, err := mgo.Dial("127.0.0.1") | |
if err != nil { | |
panic(err) | |
} | |
defer session.Close() | |
session.SetMode(mgo.Monotonic, true) | |
// Drop Database | |
if IsDrop { | |
err = session.DB("test").DropDatabase() | |
if err != nil { | |
panic(err) | |
} | |
} | |
// Collection People | |
c := session.DB("test").C("people") | |
// Index | |
index := mgo.Index{ | |
Key: []string{"name", "phone"}, | |
Unique: true, | |
DropDups: true, | |
Background: true, | |
Sparse: true, | |
} | |
err = c.EnsureIndex(index) | |
if err != nil { | |
panic(err) | |
} | |
// Insert Datas | |
err = c.Insert(&Person{Name: "Ale", Phone: "+55 53 1234 4321", Timestamp: time.Now()}, | |
&Person{Name: "Cla", Phone: "+66 33 1234 5678", Timestamp: time.Now()}) | |
if err != nil { | |
panic(err) | |
} | |
// Query One | |
result := Person{} | |
err = c.Find(bson.M{"name": "Ale"}).Select(bson.M{"phone": 0}).One(&result) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Phone", result) | |
// Query All | |
var results []Person | |
err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Results All: ", results) | |
// Update | |
colQuerier := bson.M{"name": "Ale"} | |
change := bson.M{"$set": bson.M{"phone": "+86 99 8888 7777", "timestamp": time.Now()}} | |
err = c.Update(colQuerier, change) | |
if err != nil { | |
panic(err) | |
} | |
// Query All | |
err = c.Find(bson.M{"name": "Ale"}).Sort("-timestamp").All(&results) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Results All: ", results) | |
} |
My Observation about UpdateAll() is, by default it upserts the record..
that means, if "phone" field from the change is not present in record, it will insert the field
change:= bson.M{"$set":bson.M{"phone": "+86 99 8888 7777", "timestamp": time.Now()}}
Is there a way to override the default upsert behaviour of UpdateAll() call ?
You can also do
if err := c.Update(colQuerier, change); err != nil {
// panic(err) <-- one way, or:
w.WriteHeader(404)
return
}
Hi All,
I am trying to access the record but its returning me empty record
code:
result := Person{}
err = c.Find(bson.M{"first_name": "ABC"}).Select(bson.M{"userEmail": 1}).One(&result)
if err != nil {
panic(err)
}
fmt.Println("Email", result)
output :
Email { }
please help.
Thanks for posting... learnt a lot..
Please is there a way to chain queries??? like where firstname is "anything" and lastname is "anything as well"
Thanks
Thanks a lot
thanks
how to connect mongodb+srv ?
Hi guys,
If i have probably 4 server of mongodb. 1 server active(main), and the others server are slave which the active(main) server somehow can switch as slave, and slave can switch as active(main) automatic . how i develop the code to get only the active(main) server only? active(main) server means you can do add, remove, find process in that server only. Thanks before.
Checkout this : https://docs.mongodb.com/manual/reference/method/db.currentOp/
it would be so nice if we could pass a golang struct in the find
method (the same struct which we inserted).
thanks for this
Hi All
change := bson.M{"$set": bson.M{"phone": "+86 99 8888 7777", "timestamp": time.Now()}}
I didn't get this part -- $set. What type is it? Or what are the other options in this?
I found this in docs --
I suppose inc stands for increment. But am not clear about the data type here(Interface?). Anyways again I suppose this is a fixed thing? But then that would contradict the next usage of bson.M...
Thanks in advance.