Skip to content

Commit

Permalink
feat(count): add count
Browse files Browse the repository at this point in the history
  • Loading branch information
PxyUp committed Jul 28, 2019
1 parent 25ebe5f commit f689a15
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type MongoDbConnector interface {
Connect(mongoUrl string, databaseName string) error
Disconnect()
Count(collectionName string, findPredicate bson.M) (int, error)
InsertOne(collectionName string, entity interface{}) error
UpdateOne(collectionName string, findPredicate bson.M, updatePredicate bson.M) error
UpdateAll(collectionName string, findPredicate bson.M, updatePredicate bson.M) (*mgo.ChangeInfo, error)
Expand Down
7 changes: 7 additions & 0 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func (c *MongoConnector) Disconnect() {
c.session.isConnected = false
}

func (c *MongoConnector) Count(collectionName string, findPredicate bson.M) (int, error) {
sessionCopy := c.session.session.Copy()
defer sessionCopy.Close()
collection := sessionCopy.DB(c.session.dataBaseName).C(collectionName)
return collection.Find(findPredicate).Count()
}

func (c *MongoConnector) GetIterator(collectionName string, findPredicate bson.M, opts *MongoOptions) *MongoIterator {
sessionCopy := c.session.session.Copy()
collection := sessionCopy.DB(c.session.dataBaseName).C(collectionName)
Expand Down

0 comments on commit f689a15

Please sign in to comment.