Skip to content

Commit 4461614

Browse files
authored
Merge pull request #209 from echotj/mgo_v1
add options(createcollections_options files),support abundant options(like Time Series) in mongodb5.x version
2 parents 48d13bd + b4ace96 commit 4461614

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

database.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,19 @@ func (d *Database) RunCommand(ctx context.Context, runCommand interface{}, opts
6464
}
6565
return d.database.RunCommand(ctx, runCommand, option)
6666
}
67+
68+
// CreateCollection executes a create command to explicitly create a new collection with the specified name on the
69+
// server. If the collection being created already exists, this method will return a mongo.CommandError. This method
70+
// requires driver version 1.4.0 or higher.
71+
//
72+
// The opts parameter can be used to specify options for the operation (see the options.CreateCollectionOptions
73+
// documentation).
74+
func (db *Database) CreateCollection(ctx context.Context, name string, opts ...opts.CreateCollectionOptions) error {
75+
var option = make([]*options.CreateCollectionOptions,0,len(opts))
76+
for _,opt := range opts{
77+
if opt.CreateCollectionOptions != nil{
78+
option = append(option,opt.CreateCollectionOptions)
79+
}
80+
}
81+
return db.database.CreateCollection(ctx,name,option...)
82+
}

database_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,21 @@ func TestRunCommand(t *testing.T) {
6666
{"ping", 1}}, opts)
6767
ast.NoError(res.Err())
6868
}
69+
70+
//func TestCreateCollection(t *testing.T) {
71+
// ast := require.New(t)
72+
//
73+
// cli := initClient("test")
74+
//
75+
// timeSeriesOpt := options.TimeSeriesOptions{
76+
// TimeField:"timestamp",
77+
// }
78+
// timeSeriesOpt.SetMetaField("metadata")
79+
// ctx := context.Background()
80+
// createCollectionOpts := opts.CreateCollectionOptions{CreateCollectionOptions: options.CreateCollection().SetTimeSeriesOptions(&timeSeriesOpt)}
81+
// if err := cli.CreateCollection(ctx, "syslog", createCollectionOpts); err != nil {
82+
// ast.NoError(err)
83+
// }
84+
// cli.DropCollection(ctx)
85+
// cli.DropDatabase(ctx)
86+
//}

options/createcollection_options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package options
2+
3+
import "go.mongodb.org/mongo-driver/mongo/options"
4+
5+
type CreateCollectionOptions struct {
6+
*options.CreateCollectionOptions
7+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy