diff --git a/main.go b/main.go index e7da07f..2757eb7 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/mongo/mongo.go b/mongo/mongo.go index 439f006..f780eee 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -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)