MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

mongodb c# seleziona un campo specifico

Puoi fare il prossimo:

public async Task<TValue> GetFieldValue<TEntity, TValue>(string id, Expression<Func<TEntity, TValue>> fieldExpression) where TEntity : IEntity
{
    var propertyValue = await collection
        .Find(d => d.Id == id)
        .Project(new ProjectionDefinitionBuilder<TEntity>().Expression(fieldExpression))
        .FirstOrDefaultAsync();

    return propertyValue;
}

e chiamalo

var value = await GetFieldValue<Item, string>("111", x => x.Name);