Click here to Skip to main content
15,038,451 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am doing paging of notification list using the below code,

As you can see the model has so many fields which I don't want. I just want listed id, name only to show the outer details which does not need the content or anything else. How can I ignore these fields and get the 2 fields only with minimal process from the backend ?

Paging code,

public NotificationCandidateList GetNotificationsByPagination(Guid candidateID,Guid courseID,int page, int pageSize)
                var con = new MongoClient(DBConnection.MongoDBConnectionString);
                var db = con.GetDatabase(database);
                var collection = db.GetCollection<NotificationCandidate>("NotificationCandidate");
                var countFacet = AggregateFacet.Create("countFacet",
                    PipelineDefinition<NotificationCandidate, AggregateCountResult>.Create(new[]
                var dataFacet = AggregateFacet.Create("dataFacet",
                    PipelineDefinition<NotificationCandidate, NotificationCandidate>.Create(new[]
                    PipelineStageDefinitionBuilder.Sort(Builders<NotificationCandidate>.Sort.Ascending(x => x.NotificationStatus).Descending(x=>x.CreatedDate)),
                    PipelineStageDefinitionBuilder.Skip<NotificationCandidate>((page - 1) * pageSize),
                var filter = Builders<NotificationCandidate>.Filter.Eq("candidateId", candidateID.ToString())
                          & (Builders<NotificationCandidate>.Filter.Gt("expiryDate", DateTime.Now.ToUniversalTime()));
                if (courseID != Guid.Empty)
                    filter &= Builders<NotificationCandidate>.Filter.Eq("course", courseID.ToString());
                var filtercount=filter & (Builders<NotificationCandidate>.Filter.Eq("notificationStatus", 0));
                var pendingcount = collection.Find(filtercount).CountDocuments();
                var aggregation =  collection.Aggregate()
                    .Facet(countFacet, dataFacet)
                var count = aggregation.Result.First()
                    .Facets.First(x => x.Name == "countFacet")
                    ?.Count ?? 0;
                var data = aggregation.Result.First()
                    .Facets.First(x => x.Name == "dataFacet")
                return new NotificationCandidateList
                    TotalRecords = (int)count,
                    notifications = data.ToList(),

This is my model Structure,
public class NotificationCandidate
            public Guid NotificationCandidateID { get; set; }
            public Guid NotificationID { get; set; }
            public string ShortContent { get; set; }
            public Guid CandidateID { get; set; }
            public DateTime CreatedDate { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }
            public string url { get; set; }
            public string ImageUrl { get; set; }
            public DateTime ExpiryDate { get; set; }
            public string Icon { get; set; }
            public bool FCMStatus { get; set; }
            public bool NotificationStatus { get; set; }
            public Guid ExamTypeID { get; set; }
            public int NotificationType { get; set; }
            public int Category { get; set; }
            public string ExamTypeName { get; set; }
            public BsonDocument ExtraElements { get; set; }

What I have tried:

I have tried selecting after the search and storing it to a different object, tried the object with two fields and an ignore element field but the ignore element still gets the rest of the data which I don't need from the DB.
Updated 8-Jul-21 18:51pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900