Sort By Inner Property Mongodb

89 Views Asked by At

How to sort the data by the inner property. I am very new to mongodb.

             FilterDefinitionBuilder<Products> builder = Builders<Products>.Filter;
                  FilterDefinition<Products> filter = builder.Eq(x => x.Published, true);
            
                  if (supplierId > 0)
                  {
                            filter &= builder.Eq(x => x.SupplierId, supplierId);
                  }
                  if (categoryIds is not null)
                  {
                        if (categoryIds.Contains(0))
                            categoryIds.Remove(0);
        
                        if (categoryIds.Any())
                        {
                            filter &= builder.All(x => x.AllCategoriesIds, categoryIds);
                        }
                  }
        
                  if (discountIds is not null)
                  {
                        if (discountIds.Contains(0))
                            discountIds.Remove(0);
        
                        if (discountIds.Any())
                        {
                            filter &= builder.All(x => x.AllAppliedDiscountIds, discountIds);
                        }
                  }
        
                  if (manufacturerIds is not null)
                  {
                        if (manufacturerIds.Contains(0))
                            manufacturerIds.Remove(0);
        
                        if (manufacturerIds.Any())
                        {
                            filter &= builder.All(x => x.AllManufacturerIds, manufacturerIds);
                        }
                    }
        
        
                    if (vendorIds is not null)
                    {
                        if (vendorIds.Contains(0))
                            vendorIds.Remove(0);
        
                        if (vendorIds.Any())
                        {
                            filter &= builder.Where(x => vendorIds.Contains(x.VendorId));
                        }
                    }
    
                    if (filteredSpecOptions?.Count > 0)
                    {
                        var specificationAttributeIds = filteredSpecOptions
                            .Select(sao => sao.SpecificationAttributeId)
                            .Distinct();
        
                        var optionIdsBySpecificationAttribute = filteredSpecOptions.Select(o => o.Id);
        
                        filter &= builder.All(x => x.SpecificationAttributeOptionIds, optionIdsBySpecificationAttribute);
        
                    }
        
 //var mongoPrd = _mongoRepo.GetCollection<Products>()
                    //.Find(filter).Sort(Builders<Products>.Sort
                    // .Ascending(x => x.SoldOut)
                    // .Ascending(x => x.DisableBuyButton)
                    // .Ascending(x => x.ManufacturerProductMapping.Select(x => x.DisplayOrder).FirstOrDefault())
                    // .Descending(x => x.HasBogoOfferApplied)
                    // .Descending(x => x.MaxDiscountPercentage)
                    // .Descending(x => x.HasTopSelling)
                    // .Descending(x => x.MarkAsNew)
                    // .Ascending(x => x.DisplayOrderProductTable)
                    // .Descending(x => x.ProductId));

                    //var paggedProducts = mongoPrd.Skip(pageIndex * pageSize).Limit(pageSize);

                    var pipeline = new List<BsonDocument>();
                    pipeline.Add(new BsonDocument("$match", filter.ToBsonDocument()));

                    pipeline.Add(BsonDocument.Parse("{ $unwind: \"$ManufacturerProductMapping\" }"));

                    pipeline.Add(BsonDocument.Parse("{ $addFields: { DisplayOrder: { $cond: { if: { $isArray: \"$ManufacturerProductMapping.DisplayOrder\" }, then: { $arrayElemAt: [ \"$ManufacturerProductMapping.DisplayOrder\", 0 ] }, else: -1 } } } }"));

                    pipeline.Add(BsonDocument.Parse("{ $sort: { SoldOut: 1, DisableBuyButton: 1, HasBogoOfferApplied: -1, MaxDiscountPercentage: -1, HasTopSelling: -1, MarkAsNew: -1, DisplayOrderProductTable: 1, ProductId: -1 } }"));

                    pipeline.Add(BsonDocument.Parse("{ $skip: " + (pageIndex * pageSize) + " }"));

                    pipeline.Add(BsonDocument.Parse("{ $limit: " + pageSize + " }"));

                    var mongoPrd = _mongoRepo.GetCollection<Products>().Aggregate<BsonDocument>(pipeline);

The match is not working but without the match, it is working properly.

Below is the collection. enter image description here

0

There are 0 best solutions below