alexandre-spieser / mongodb-generic-repository

An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
MIT License
315 stars 87 forks source link

Question: is it possible to get only some fields on select ? #32

Closed boiss-dev closed 4 years ago

boiss-dev commented 4 years ago

Hello, first of all congrats for your great work.

is it possible to do something like select Name, Color from Cars;

instead of select * from Cars;

thanks.

alexandre-spieser commented 4 years ago

Hi, yes you can use the Projection methods.

https://github.com/alexandre-spieser/mongodb-generic-repository/blob/c92a126ecc5e772e3601a49c7c9c6c26cc24b627/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs

Tests for usage example can be seen here:

https://github.com/alexandre-spieser/mongodb-generic-repository/blob/54b2a63a8e2a2cf0e81a4755882b200bf58c270e/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs#L646

        [Fact]
        public async Task ProjectOneAsync()
        {
            // Arrange
            var someContent = GetContent();
            var someDate = DateTime.UtcNow;
            var document = CreateTestDocument();
            document.SomeContent = someContent;
            document.Nested.SomeDate = someDate;
            SUT.AddOne<T>(document);
            // Act
            var result = await SUT.ProjectOneAsync<T, MyTestProjection>(
                x => x.Id.Equals(document.Id),
                x => new MyTestProjection
                {
                    SomeContent = x.SomeContent,
                    SomeDate = x.Nested.SomeDate
                },
                PartitionKey);
            // Assert
            Assert.True(null != result, GetTestName());
            Assert.True(someContent == result.SomeContent, GetTestName());
            Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
            Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
        }
boiss-dev commented 4 years ago

cool ! I will test that. thanks for your support

boiss-dev commented 4 years ago

works like a charm. thanks a lot