This project has moved. For the latest updates, please go here.

Expose MongoCursor methods?

Mar 26, 2012 at 12:34 PM

I would like to request that the various MongoCollection methods that return a MongoCursor, be implemented in the MongoRepository class.

Currently I can get access to those through the obsoleted MongoCollection<T> Collection property, but I'm concerned about how those will be accessed once this Property is removed from future releases.

The two MongoCursor methods that I think would be appropriate for your MongoRepository class would be:

        public MongoCursor FindAll()        {            return this.collection.FindAll();        }

        public MongoCursor Find(IMongoQuery query)        {            return this.collection.Find(query);        }

I'm fairly new to Mongo, so I definitely am open to ideas if there is a better strategy for getting to this.


Paul Brower

Apr 1, 2012 at 5:47 PM

I have ran into similar issues and have even resorted to using the obsoleted Collection property. I am not sure on what would be best to do, especially since the 1.4 driver is on the verge of being released which could change this problem alltogether. I will ponder the issue here and decide what to do as soon as 1.4 is released. Deal?

Apr 1, 2012 at 9:57 PM

I have created a full implementation of the below interface.  If any part of my implementation would help, I'd be happy to share/coordinate on this.  I'm an avid believer in open-source, and was planning on posting my changes anyway on github.  Let me know what I can do.


    public interface IRepositoryMongo<T> where T : class, IEntity
        void Add(IEnumerable<T> entities);
        void Add(T entity);
        IQueryable<T> All();
        IQueryable<T> All(Expression<Func<T, bool>> criteria);
        long Count();

        IQueryable<T> FindDeleted();
        IQueryable<T> FindDeleted(Expression<Func<T, bool>> criteria);
        IQueryable<T> FindRevisions(Expression<Func<T, bool>> criteria);
        void DeleteFromAuditing(MongoDocumentAuditEnum auditType,  Expression<Func<T, bool>> criteria);
        void DeleteFromAuditing(MongoDocumentAuditEnum auditType,  string id);

        void Delete(T entity);
        void Delete(T entity, bool ignoreSavingToDeletedObject);
        void Delete(Expression<Func<T, bool>> criteria, bool savedCopyOfDeletedItems);
        void Delete(Expression<Func<T, bool>> criteria);
        void DeleteAll();
        bool Equals(object obj);
        bool Exists(Expression<Func<T, bool>> criteria);
        T GetById(string id);
        T GetSingle(Expression<Func<T, bool>> criteria);
        void RequestDone();
        IDisposable RequestStart();
        IDisposable RequestStart(bool slaveOk);
        T Update(T entity);
        void Update(IEnumerable<T> entities);

        /// <summary>
        /// Drops the collection
        /// </summary>
        void Drop();
        bool Exists();
        CollectionStatsResult GetStats();
        ValidateCollectionResult Validate();

        long GetTotalDataSize();
        long GetTotalStorageSize();
        bool IsCapped();

        void Reindex();
        GetIndexesResult GetIndexes();

        void ReindexIndexCache();
        void EnsureIndexes(IMongoIndexKeys indexKeys, IMongoIndexOptions indexOptions);
        bool IndexExists(string indexName);
        bool IndexesExist(IEnumerable<string> indexNames);
        void DropIndex(string indexName);
        void DropIndexes(IEnumerable<string> indexNames );
        void DropAllIndexes();

        bool Undelete(string id);

        MongoCursor<T> FindAll();
        MongoCursor<T> Find(IMongoQuery query);
        MongoCursor<Y> FindAs<Y>(IMongoQuery query);
        MongoCursor<Y> FindAllAs<Y>();

        T MapReduce(string map, string reduce);


Apr 1, 2012 at 10:24 PM
Edited Apr 1, 2012 at 10:28 PM

I'm sorry but methods like FindDeleted, FindRevisions, DeleteFromAuditing, Delete(T entity, bool ignoreSavingToDeletedObject) or Undelete are very specific to some (probably your) project; when you would want to use these kinds of methods I would suggest deriving your own classes. It's not good design to stuff everything someone might actually need into one class; also each person/project would require their own specific implementation of these methods. What we're trying to do is provide an abstraction of the MongoDB specific details in a very generic repository pattern.

For the GetIndexes, EnsureIndexes, IndexExists, DropIndex(es), DropAllIndexes and several other methods you should have a look at the documentation under Managing the underlying MongoCollection. We already provide a MongoRepositoryManager class that exposes all these methods.

Don't get me wrong; input is very welcome but the changes you're proposing are not changes that everyone would benefit from and very specific to your and other peoples needs. You should derive your own class as you would for implementing methods like CustomerRepository.FindByZipcode() or ReservationsRepository.DeleteByReservationId(). As for the other methods and as explained in the previous paragraph: most of them are already exposed in de MongoRepositoryManager.

I am, however, still considering on what to do with the MongoCursor and how to wrestle that into a repository-ish solution (if possible at all).

Apr 1, 2012 at 10:30 PM

And YAY! I must've missed it but 1.4 has been released a few days ago.

Apr 1, 2012 at 10:33 PM

Yes, I've been messing with it.  I knew I had some things in that interface specific for me ... thanks for pointing out (and reminding me) about the single responsibility principle!

Since you're already exposing SOME of the features of a MongoCollection in your project, I did think it made sense to expose the rest (such as the method that return a MongoCursor ... I look forward to seeing revisions.  thanks.

Apr 1, 2012 at 11:03 PM
Edited Apr 1, 2012 at 11:06 PM

Crap; just tried 1.4 but it seems we need to wait a little more for 1.4.1 to be released: 

It is nice to be able to get rid of FluentMongo (altough it had the same author as who was responsible for the Linq support in the driver anyway) and I'm looking into the other improvements and how we can benefit from those.


"Since you're already exposing SOME of the features of a MongoCollection in your project, I did think it made sense to expose the rest (such as the method that return a MongoCursor ..."

I'm trying hard to not "leak" any MongoDB specific implementation details in the MongoRepository class (that's why the Collection property is obsoleted in the first place) but because we also need to be pragmatic and just have to deal with some details sometimes I've put as much of it as possible in the MongoRepositoryManager class.