Skip to content

Commit

Permalink
Merge pull request qiniu#209 from echotj/mgo_v1
Browse files Browse the repository at this point in the history
add options(createcollections_options files),support abundant options(like Time Series) in mongodb5.x version
  • Loading branch information
jiangz222 authored Oct 22, 2021
2 parents 48d13bd + b4ace96 commit 4461614
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,19 @@ func (d *Database) RunCommand(ctx context.Context, runCommand interface{}, opts
}
return d.database.RunCommand(ctx, runCommand, option)
}

// CreateCollection executes a create command to explicitly create a new collection with the specified name on the
// server. If the collection being created already exists, this method will return a mongo.CommandError. This method
// requires driver version 1.4.0 or higher.
//
// The opts parameter can be used to specify options for the operation (see the options.CreateCollectionOptions
// documentation).
func (db *Database) CreateCollection(ctx context.Context, name string, opts ...opts.CreateCollectionOptions) error {
var option = make([]*options.CreateCollectionOptions,0,len(opts))
for _,opt := range opts{
if opt.CreateCollectionOptions != nil{
option = append(option,opt.CreateCollectionOptions)
}
}
return db.database.CreateCollection(ctx,name,option...)
}
18 changes: 18 additions & 0 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,21 @@ func TestRunCommand(t *testing.T) {
{"ping", 1}}, opts)
ast.NoError(res.Err())
}

//func TestCreateCollection(t *testing.T) {
// ast := require.New(t)
//
// cli := initClient("test")
//
// timeSeriesOpt := options.TimeSeriesOptions{
// TimeField:"timestamp",
// }
// timeSeriesOpt.SetMetaField("metadata")
// ctx := context.Background()
// createCollectionOpts := opts.CreateCollectionOptions{CreateCollectionOptions: options.CreateCollection().SetTimeSeriesOptions(&timeSeriesOpt)}
// if err := cli.CreateCollection(ctx, "syslog", createCollectionOpts); err != nil {
// ast.NoError(err)
// }
// cli.DropCollection(ctx)
// cli.DropDatabase(ctx)
//}
7 changes: 7 additions & 0 deletions options/createcollection_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package options

import "go.mongodb.org/mongo-driver/mongo/options"

type CreateCollectionOptions struct {
*options.CreateCollectionOptions
}

0 comments on commit 4461614

Please sign in to comment.