Creator
The Creator() method allows us to obtain a new generic creator object, namely Creator[T]. With the methods of Creator[T], we can execute relevant creation operations.
Insert a single document
go
insertOneResult, err := userColl.Creator().InsertOne(context.Background(), &User{Name: "Mingyong Chen", Age: 18})- The
InsertOnemethod is used to insert a single document. TheinsertOneResultis of the type*mongo.InsertOneResult. The second argument of theInsertOnemethod is a pointer object of the specified generic type, i.e.,*User. - If we need to set the
optionsparameter, we can pass it as the third parameter of the method.
Insert multiple documents
go
users := []*User{
{Name: "Mingyong Chen", Age: 18},
{Name: "Burt", Age: 18},
}
insertManyResult, err := userColl.Creator().InsertMany(context.Background(), users)- The
InsertManymethod is used to insert multiple documents. TheinsertManyResultis of the type*mongo.InsertManyResult. The second argument of theInsertManymethod is a slice object of the specified generic type, with the elements of the slice being pointer types, i.e.,[]*User. - If we need to set the
optionsparameter, we can pass it as the third parameter of the method.