import(
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
MongoConnect := MongodbConnect()
var modelsWrite []mongo.WriteModel
for i := 1; i < 100; i++ {
insertDataMap := make(map[string]interface{})
insertDataMap["one_id"] = 1
insertDataMap["tow_user_name"] = userName
userName := "hash insertDataMap"
timeNow := time.Now().Format("2006-01-02 15:04:05")
filterData := bson.D{
{"one_id", i},
{"hash_string", hashStringData},
}
user := fmt.Sprintf("Man_%s", timeNow)
updateData := bson.D{
{"$set", bson.D{
{"tow_user_name", userName},
{"three_user_gender", 0},
{"regist_time", timeNow},
{"login_time", timeNow},
}},
{"$setOnInsert", bson.D{
{"regist_time", timeNow},
}},
}
modelsWrite = append(
modelsWrite,
mongo.NewUpdateManyModel().SetFilter(filterData).SetUpdate(updateData).SetUpsert(true),
)
}
ops := options.BulkWrite().SetOrdered(false)
retInsert, err := MongoCon.Collection(table).BulkWrite(ctx, modelsWrite, ops)
if err != nil {
log.Debugf("InsertOrUpdateUserData error, err:%+v", err)
}
log.Infof("InsertOrUpdateUserData success return %+v", retInsert)