sql >> Base de Datos >  >> NoSQL >> MongoDB

no se puede convertir de 'MongoDB.Driver.IMongoCollection<>' a 'System.Collections.Generic.IEnumerable<>'

En el nuevo MongoDB Driver, ahora todo se basa en métodos asincrónicos, por lo que ya no se aplican los métodos antiguos para consultar datos.

Básicamente, le gustaría crear una clase MongoRepository, con un método de búsqueda, y ese repositorio podría tener el siguiente método de búsqueda:

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();
    }

}

Esto podría implementarse así:

MongoRepository<Registration> repo = new MongoRepository("Registrations");
IList<Registration> registrations = repo.Find(i => i.SomeProperty == true);

Hay buena información sobre cómo se pueden implementar los cambios en la API aquí:http://mongodb.github.io/mongo-csharp-driver/2.0/upgrading/