@@ -157,9 +157,9 @@ const (
157
157
// topology.
158
158
//
159
159
// Dial will timeout after 10 seconds if a server isn't reached. The returned
160
- // session will timeout operations after one minute by default if servers
161
- // aren't available. To customize the timeout, see DialWithTimeout,
162
- // SetSyncTimeout, and SetSocketTimeout.
160
+ // session will timeout operations after one minute by default if servers aren't
161
+ // available. To customize the timeout, see DialWithTimeout, SetSyncTimeout, and
162
+ // SetSocketTimeout.
163
163
//
164
164
// This method is generally called just once for a given cluster. Further
165
165
// sessions to the same cluster are then established using the New or Copy
@@ -184,8 +184,8 @@ const (
184
184
// If the port number is not provided for a server, it defaults to 27017.
185
185
//
186
186
// The username and password provided in the URL will be used to authenticate
187
- // into the database named after the slash at the end of the host names, or
188
- // into the "admin" database if none is provided. The authentication information
187
+ // into the database named after the slash at the end of the host names, or into
188
+ // the "admin" database if none is provided. The authentication information
189
189
// will persist in sessions obtained through the New method as well.
190
190
//
191
191
// The following connection options are supported after the question mark:
@@ -235,6 +235,10 @@ const (
235
235
// Defines the per-server socket pool limit. Defaults to 4096.
236
236
// See Session.SetPoolLimit for details.
237
237
//
238
+ // appName=<appName>
239
+ //
240
+ // The identifier of this client application. This parameter is used to
241
+ // annotate logs / profiler output and cannot exceed 128 bytes.
238
242
//
239
243
// Relevant documentation:
240
244
//
@@ -279,6 +283,7 @@ func ParseURL(url string) (*DialInfo, error) {
279
283
source := ""
280
284
setName := ""
281
285
poolLimit := 0
286
+ appName := ""
282
287
readPreferenceMode := Primary
283
288
var readPreferenceTagSets []bson.D
284
289
for _ , opt := range uinfo .options {
@@ -296,6 +301,11 @@ func ParseURL(url string) (*DialInfo, error) {
296
301
if err != nil {
297
302
return nil , errors .New ("bad value for maxPoolSize: " + opt .value )
298
303
}
304
+ case "appName" :
305
+ if len (opt .value ) > 128 {
306
+ return nil , errors .New ("appName too long, must be < 128 bytes: " + opt .value )
307
+ }
308
+ appName = opt .value
299
309
case "readPreference" :
300
310
switch opt .value {
301
311
case "nearest" :
@@ -350,6 +360,7 @@ func ParseURL(url string) (*DialInfo, error) {
350
360
Service : service ,
351
361
Source : source ,
352
362
PoolLimit : poolLimit ,
363
+ AppName : appName ,
353
364
ReadPreference : & ReadPreference {
354
365
Mode : readPreferenceMode ,
355
366
TagSets : readPreferenceTagSets ,
@@ -409,6 +420,9 @@ type DialInfo struct {
409
420
// See Session.SetPoolLimit for details.
410
421
PoolLimit int
411
422
423
+ // The identifier of the client application which ran the operation.
424
+ AppName string
425
+
412
426
// ReadPreference defines the manner in which servers are chosen. See
413
427
// Session.SetMode and Session.SelectServers.
414
428
ReadPreference * ReadPreference
@@ -472,7 +486,7 @@ func DialWithInfo(info *DialInfo) (*Session, error) {
472
486
}
473
487
addrs [i ] = addr
474
488
}
475
- cluster := newCluster (addrs , info .Direct , info .FailFast , dialer {info .Dial , info .DialServer }, info .ReplicaSetName )
489
+ cluster := newCluster (addrs , info .Direct , info .FailFast , dialer {info .Dial , info .DialServer }, info .ReplicaSetName , info . AppName )
476
490
session := newSession (Eventual , cluster , info .Timeout )
477
491
session .defaultdb = info .Database
478
492
if session .defaultdb == "" {
@@ -652,6 +666,30 @@ func (db *Database) C(name string) *Collection {
652
666
return & Collection {db , name , db .Name + "." + name }
653
667
}
654
668
669
+ // CreateView creates a view as the result of the applying the specified
670
+ // aggregation pipeline to the source collection or view. Views act as
671
+ // read-only collections, and are computed on demand during read operations.
672
+ // MongoDB executes read operations on views as part of the underlying aggregation pipeline.
673
+ //
674
+ // For example:
675
+ //
676
+ // db := session.DB("mydb")
677
+ // db.CreateView("myview", "mycoll", []bson.M{{"$match": bson.M{"c": 1}}}, nil)
678
+ // view := db.C("myview")
679
+ //
680
+ // Relevant documentation:
681
+ //
682
+ // https://docs.mongodb.com/manual/core/views/
683
+ // https://docs.mongodb.com/manual/reference/method/db.createView/
684
+ //
685
+ func (db * Database ) CreateView (view string , source string , pipeline interface {}, collation * Collation ) error {
686
+ command := bson.D {{"create" , view }, {"viewOn" , source }, {"pipeline" , pipeline }}
687
+ if collation != nil {
688
+ command = append (command , bson.DocElem {"collation" , collation })
689
+ }
690
+ return db .Run (command , nil )
691
+ }
692
+
655
693
// With returns a copy of db that uses session s.
656
694
func (db * Database ) With (s * Session ) * Database {
657
695
newdb := * db
@@ -1499,6 +1537,29 @@ func (c *Collection) DropIndexName(name string) error {
1499
1537
return nil
1500
1538
}
1501
1539
1540
+ // DropAllIndexes drops all the indexes from the c collection
1541
+ func (c * Collection ) DropAllIndexes () error {
1542
+ session := c .Database .Session
1543
+ session .ResetIndexCache ()
1544
+
1545
+ session = session .Clone ()
1546
+ defer session .Close ()
1547
+
1548
+ db := c .Database .With (session )
1549
+ result := struct {
1550
+ ErrMsg string
1551
+ Ok bool
1552
+ }{}
1553
+ err := db .Run (bson.D {{"dropIndexes" , c .Name }, {"index" , "*" }}, & result )
1554
+ if err != nil {
1555
+ return err
1556
+ }
1557
+ if ! result .Ok {
1558
+ return errors .New (result .ErrMsg )
1559
+ }
1560
+ return nil
1561
+ }
1562
+
1502
1563
// nonEventual returns a clone of session and ensures it is not Eventual.
1503
1564
// This guarantees that the server that is used for queries may be reused
1504
1565
// afterwards when a cursor is received.
@@ -1512,19 +1573,6 @@ func (session *Session) nonEventual() *Session {
1512
1573
1513
1574
// Indexes returns a list of all indexes for the collection.
1514
1575
//
1515
- // For example, this snippet would drop all available indexes:
1516
- //
1517
- // indexes, err := collection.Indexes()
1518
- // if err != nil {
1519
- // return err
1520
- // }
1521
- // for _, index := range indexes {
1522
- // err = collection.DropIndex(index.Key...)
1523
- // if err != nil {
1524
- // return err
1525
- // }
1526
- // }
1527
- //
1528
1576
// See the EnsureIndex method for more details on indexes.
1529
1577
func (c * Collection ) Indexes () (indexes []Index , err error ) {
1530
1578
cloned := c .Database .Session .nonEventual ()
0 commit comments