Home > Enterprise >  MongoDB Select By Group along with that Count Unique match exclude array and object fields Get data
MongoDB Select By Group along with that Count Unique match exclude array and object fields Get data

Time:08-01

I have a collection where from the backend user can input multiple same name bikes but with different registration number but in front-End I want them to be grouped by matching the same name but as user updates separately display image changes but I want only one display image as it is 1 vehicle

provided there is a node created I will implement it we can sort it by the latest and take the price and image of it

  1. Activa -2 Count
  2. KTM -1 Count

but there is a catch.

Activa 2 bikes but I want only count 2 and the price as it is the same in an array I want only 1 and the same applies to displayimage here display image file path is different but I want the latest one only Sharing data below

Data:

[
    {
        "price": [
            {
                "Description": "Hourly",
                "Price": "1"
            },
            {
                "Description": "Daily",
                "Price": "11"
            },
            {
                "Description": "Monthly",
                "Price": "111"
            }
        ],
        "_id": "62e69ee3edfe4d0f3cb4994a",
        "bikename": "KTM",
        "bikenumber": "KA05HM2034",
        "bikebrand": {
            "id": 1,
            "label": "Honda"
        },
        "freekm": 234,
        "displayimage": {
            "file": "bike-2020-honda-city-exterior-8-1659281111883.jpg",
            "file_path": "https://www.example.com/images/upload/bike-2020-honda-city-exterior-8-1659281111883.jpg",
            "idx": 1
        }
    },
    {
        "price": [
            {
                "Description": "Hourly",
                "Price": "1"
            },
            {
                "Description": "Daily",
                "Price": "11"
            },
            {
                "Description": "Monthly",
                "Price": "111"
            }
        ],
        "_id": "62dba8418ef8f51f454ed757", 
        "bikename": "Activa",
        "bikenumber": "KA05HM2033",
        "bikebrand": {
            "id": 1,
            "label": "Honda"
        }, 
        "freekm": 234, 
        "displayimage": {
            "file": "bike-v_activa-i-deluxe-1658562557459.jpg",
            "file_path": "https://www.example.com/images/upload/bike-v_activa-i-deluxe-1658562557459.jpg",
            "idx": 0
        }
    },
    {
        "price": [
            {
                "Description": "Hourly",
                "Price": "1"
            },
            {
                "Description": "Daily",
                "Price": "11"
            },
            {
                "Description": "Monthly",
                "Price": "111"
            }
        ],
        "_id": "62d7ff7e70b9ab38c6ab0cb1", 
        "bikename": "Activa",
        "bikenumber": "KA05HM2223",
        "bikebrand": {
            "id": 1,
            "label": "Honda"
        }, 
        "freekm": 234,
        "afterfreekmprice": 22,
        "descreption": "Activa",
        "displayimage": {
            "file": "bike-v_activa-i-deluxe-1658322798414.jpg",
            "file_path": "https://www.example.com/images/upload/bike-v_activa-i-deluxe-1658322798414.jpg",
            "idx": 0
        }
    }
]

Expected:

[
    {
        "_id":{
            "price": [
                {
                    "Description": "Hourly",
                    "Price": "1"
                },
                {
                    "Description": "Daily",
                    "Price": "11"
                },
                {
                    "Description": "Monthly",
                    "Price": "111"
                }
            ],
            "_id": "62dba8418ef8f51f454ed757", 
            "bikename": "Activa",
            "bikebrand": {
                "id": 1,
                "label": "Honda"
            }, 
            "freekm": 234, 
            "displayimage": {
                "file": "bike-v_activa-i-deluxe-1658562557459.jpg",
                "file_path": "https://www.example.com/images/upload/bike-v_activa-i-deluxe-1658562557459.jpg",
                "idx": 0
            }
        },
        "count": 2
    },
    {
        "_id":{
            "price": [
                {
                    "Description": "Hourly",
                    "Price": "1"
                },
                {
                    "Description": "Daily",
                    "Price": "11"
                },
                {
                    "Description": "Monthly",
                    "Price": "111"
                }
            ],
            "_id": "62e69ee3edfe4d0f3cb4994a",
            "bikename": "KTM",
            "bikebrand": {
                "id": 1,
                "label": "Honda"
            },
            "freekm": 234,
            "displayimage": {
                "file": "bike-2020-honda-city-exterior-8-1659281111883.jpg",
                "file_path": "https://www.example.com/images/upload/bike-2020-honda-city-exterior-8-1659281111883.jpg",
                "idx": 1
            }
        }
        "count": 1
    }
] 

CodePudding user response:

You can use $group for this:

db.collection.aggregate([
  {$group: {
      _id: "$bikename",
      count: {$sum: 1},
      data: {$first: "$$ROOT"}
    }
  },
  {$set: {"data.count": "$count"}},
  {$replaceRoot: {newRoot: "$data"}}
])

See how it works on the playground example

CodePudding user response:

You can use the aggregation pipeline,

  • $sort by _id in descending order
  • $group by bikename and get the first root document that is latest one in root and count total documents in count
  • $project to show required documents
db.collection.aggregate([
  { $sort: { _id: -1 } },
  {
    $group: {
      _id: "$bikename",
      root: { $first: "$$ROOT" },
      count: { $sum: 1 }
    }
  },
  {
    $project: {
      _id: "$root",
      count: 1
    }
  }
])

Playground

  • Related