# 插入一条数据:
| func main() { |
| var ( |
| client = mongodb.MgoCli() |
| err error |
| collection *mongo.Collection |
| iResult *mongo.InsertOneResult |
| id primitive.ObjectID |
| ) |
| |
| collection = client.Database("my_db").Collection("my_collection") |
| |
| |
| logRecord := model.LogRecord{ |
| JobName: "job1", |
| Command: "echo 1", |
| Err: "", |
| Content: "1", |
| Tp: model.ExecTime{ |
| StartTime: time.Now().Unix(), |
| EndTime: time.Now().Unix() + 10, |
| }, |
| } |
| if iResult, err = collection.InsertOne(context.TODO(), logRecord); err != nil { |
| fmt.Print(err) |
| return |
| } |
| |
| id = iResult.InsertedID.(primitive.ObjectID) |
| fmt.Println("自增ID", id.Hex()) |
| } |
# 插入多条数据
| func main() { |
| var ( |
| client = mongodb.MgoCli() |
| err error |
| collection *mongo.Collection |
| result *mongo.InsertManyResult |
| id primitive.ObjectID |
| ) |
| collection = client.Database("my_db").Collection("table1") |
| |
| |
| result, err = collection.InsertMany(context.TODO(), []interface{}{ |
| model.LogRecord{ |
| JobName: "job multi1", |
| Command: "echo multi1", |
| Err: "", |
| Content: "1", |
| Tp: model.ExecTime{ |
| StartTime: time.Now().Unix(), |
| EndTime: time.Now().Unix() + 10, |
| }, |
| }, |
| model.LogRecord{ |
| JobName: "job multi2", |
| Command: "echo multi2", |
| Err: "", |
| Content: "2", |
| Tp: model.ExecTime{ |
| StartTime: time.Now().Unix(), |
| EndTime: time.Now().Unix() + 10, |
| }, |
| }, |
| }) |
| if err != nil{ |
| log.Fatal(err) |
| } |
| if result == nil { |
| log.Fatal("result nil") |
| } |
| for _, v := range result.InsertedIDs { |
| id = v.(primitive.ObjectID) |
| fmt.Println("自增ID", id.Hex()) |
| } |
| } |
执行 Insert
命令后,还要再执行
| for _, v := range result.InsertedIDs { |
| id = v.(primitive.ObjectID) |
| fmt.Println("自增ID", id.Hex()) |
| } |
使用 bson
设置 ObjectID
# 设置 find limit
| func main() { |
| var ( |
| client = mongodb.MgoCli() |
| collection *mongo.Collection |
| err error |
| cursor *mongo.Cursor |
| ) |
| collection = client.Database("my_db").Collection("table1") |
| filter := bson.M{"jobName":"job multi1"} |
| if cursor, err = collection.Find(context.TODO(), filter,options.Find().SetSkip(0), options.Find().SetLimit(2) ); err != nil { |
| log.Fatal(err) |
| } |
| defer func() { |
| if err = cursor.Close(context.TODO()); err != nil { |
| log.Fatal(err) |
| } |
| }() |
| |
| var results []model.LogRecord |
| if err = cursor.All(context.TODO(), &results); err != nil { |
| log.Fatal(err) |
| } |
| for _, result := range results { |
| fmt.Println(result) |
| } |
| } |
# update
| func main() { |
| var ( |
| client = mongodb.MgoCli() |
| collection *mongo.Collection |
| err error |
| uResult *mongo.UpdateResult |
| ) |
| collection = client.Database("my_db").Collection("table1") |
| filter := bson.M{"jobName": "job multi1"} |
| update := bson.M{"$set": model. |
| UpdateByJobName{Command: "byModel",Content:"model"}} |
| if uResult, err = collection. |
| UpdateMany(context.TODO(), filter, update); err != nil { |
| log.Fatal(err) |
| } |
| |
| log.Println(uResult.MatchedCount) |
| } |
# 删除数据
| sql := `{"name":"jack"}` |
| var bs interface{} |
| bson.UnmarshalExtJSON([]byte(sql), true,&bs) |
| collection.DeleteMany(context.TODO(), bs) |
同样的,先 sqlstr
转 interface
, 然而直接传入参数。