MongoDB
 sql >> Base de Dados >  >> NoSQL >> MongoDB

não é possível converter de 'MongoDB.Driver.IMongoCollection<>' para 'System.Collections.Generic.IEnumerable<>'


No novo Driver MongoDB, tudo agora é baseado em métodos assíncronos, portanto, os métodos antigos para consultar dados não se aplicam mais.

Basicamente, você desejaria criar uma classe MongoRepository, com um método find, e esse repositório poderia ter o seguinte método Find:
public class MongoRepository<T>
{

    protected IMongoCollection<T> _collection;

    public MongoRepository(string collectionName) 
    {
        // Get your mongo client and database objects here.
        _collection = _mongoDb.GetCollection<T>(collectionName);
    }

    public async Task<IList<T>> Find(Expression<Func<T, bool>> query)
    {
        // Return the enumerable of the collection
        return await _collection.Find<T>(query).ToListAsync();
    }

}

Isso poderia então ser implementado assim:
MongoRepository<Registration> repo = new MongoRepository("Registrations");
IList<Registration> registrations = repo.Find(i => i.SomeProperty == true);

Há algumas boas informações sobre como as alterações na API podem ser implementadas aqui:http://mongodb.github.io/mongo-csharp-driver/2.0/upgrading/