//链接服务器
session, err := mgo.Dial("114.55.248.252:12345")
iferr != nil {
panic(err)
}
defersession.Close()
//// Optional. Switch the session to a monotonic behavior.
//session.SetMode(mgo.Monotonic, true)
//链接mogdb
c := session.DB("test").C("test")
fmt.Println(c)
////插入
//obj := &Person{}
//obj.Title = "haha"
//obj.Value = "0000"
//
//obj1 := &Person{}
//obj1.Title = "222"
//obj1.Value = "2222"
//
//err = c.Insert(obj,obj1)
//fmt.Println(err)
//if err != nil {
// panic(err)
//}
//
////删除
////c.Remove(bson.M{"_id": bson.M{"$gte": bson.MongoTimestamp("18")}})
////_, 忽略
//_, err = c.RemoveAll(bson.M{"title": "222"})
//if err != nil {
// fmt.Println(err)
//}
//读取、查找
//result Person{}
//err = c.Find(bson.M{"title": "222"}).One(&result)
//if err != nil {
// panic(err)
//}
//
//fmt.Println("title:", result.Title)
//修改字段的值($set)bson.ObjectIdHex("5204af979955496907000001")
c.Update(bson.M{"title":"11111"},
bson.M{"$set": bson.M{
"value":"11111",
"age":34,
}})
//字段增加值.如果表里有这个值就在原有基础上加减,没有的话会增加一个字段
c.Update(bson.M{"title":"11111"},
bson.M{"$inc": bson.M{
"agee":10,
}})
网友评论