Click here to Skip to main content
15,881,092 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,

C#
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[]
                    {
                    PipelineStageDefinitionBuilder.Count<NotificationCandidate>()
                    }));
    
                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),
                    PipelineStageDefinitionBuilder.Limit<NotificationCandidate>(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()
                    .Match(filter)
                    .Facet(countFacet, dataFacet)
                    .ToListAsync();
    
                var count = aggregation.Result.First()
                    .Facets.First(x => x.Name == "countFacet")
                    .Output<AggregateCountResult>()
                    ?.FirstOrDefault()
                    ?.Count ?? 0;
    
                var data = aggregation.Result.First()
                    .Facets.First(x => x.Name == "dataFacet")
                    .Output<NotificationCandidate>());
    
                return new NotificationCandidateList
                {
                    TotalRecords = (int)count,
                    notifications = data.ToList(),
                    PendingCount=pendingcount
                };
            }


This is my model Structure,
C#
public class NotificationCandidate
        {
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("notificationCandidateId")]
            public Guid NotificationCandidateID { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("notificationId")]
            public Guid NotificationID { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("shortContent")]
            public string ShortContent { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("candidateId")]
            public Guid CandidateID { get; set; }
    
            [BsonRepresentation(BsonType.DateTime)]
            [BsonElement("createdDate")]
            public DateTime CreatedDate { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("title")]
            public string Title { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("content")]
            public string Content { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("url")]
            public string url { get; set; }
            [BsonRepresentation(BsonType.String)]
            [BsonElement("imageUrl")]
            public string ImageUrl { get; set; }
    
            [BsonRepresentation(BsonType.DateTime)]
            [BsonElement("expiryDate")]
            public DateTime ExpiryDate { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("icon")]
            public string Icon { get; set; }
    
            [BsonRepresentation(BsonType.Int32)]
            [BsonElement("fcmStatus")]
            public bool FCMStatus { get; set; }
    
            [BsonRepresentation(BsonType.Int32)]
            [BsonElement("notificationStatus")]
            public bool NotificationStatus { get; set; }
    
            [BsonRepresentation(BsonType.String)]
            [BsonElement("course")]
            public Guid ExamTypeID { get; set; }
    
            [BsonRepresentation(BsonType.Int32)]
            [BsonElement("type")]
            public int NotificationType { get; set; }
    
            [BsonRepresentation(BsonType.Int32)]
            [BsonElement("commonCategory")]
            public int Category { get; set; }
            [BsonRepresentation(BsonType.String)]
            [BsonElement("courseName")]
            public string ExamTypeName { get; set; }
    
    
            [BsonIgnoreIfDefault]
            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.
Posted
Updated 8-Jul-21 18:51pm
v3

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