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

Text Indexes and Capped Collections

May 6, 2014 at 5:46 AM
Hi,

Is there a way to create text indexes and capped collections?
I've found the MongoRepositoryManager, but it doesn't appear to support either.

Cheers
Coordinator
May 6, 2014 at 10:44 AM
Edited May 6, 2014 at 11:07 AM
You can easily implement a capped repository:
using MongoDB.Driver.Builders;
using MongoRepository;

public class CappedRepository<T> : MongoRepository<T>
        where T : Entity
{
    public CappedRepository(long maxSize, long maxDocuments)
    {
        var options = CollectionOptions
           .SetCapped(true)
           .SetMaxSize(maxSize)
           .SetMaxDocuments(maxDocuments);

        var db = this.collection.Database;

        if (!db.CollectionExists(this.Collection.Name))
            db.CreateCollection(this.Collection.Name, options);
    }
}
Then do:
var myCappedRepo = new CappedRepository<Foo>(100, 50);
The text-index can be implemented in a similar way:
using MongoDB.Driver.Builders;
using MongoRepository;


public class MyRepository : MongoRepository<Foo>
{
    public MyRepository()
    {
        this.collection.EnsureIndex(new IndexKeysBuilder().Text("some-field-name"));
    }
}
Note that for both cases (capped collection and text-index) you need to reference some MongoDB's actual driver.
Marked as answer by Cyonix on 5/6/2014 at 3:27 PM
May 6, 2014 at 11:27 PM
Great, thanks for that!