1

I am trying to create top 10 product list based on postType = "buy". My logic is a count postType = "buy" and sort the top 10 products from the logs collection. Here are my sample log collections.

[
    {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "3",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "view",
        "product": "4",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "2",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "view",
        "product": "2",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "share",
        "product": "3",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "2",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "2",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "share",
        "product": "2",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "1",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "1",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "viewvideo",
        "product": "1",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "viewvideo",
        "product": "2",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "viewvideo",
        "product": "3",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "4",
    },
 {
        "_id": "633dc5b761ff04e7ae8e8c0f",
        "postType": "buy",
        "product": "5",
    }
]

My expected output is:

 [{"product":1, "count":2},{"product":2, "count":3},{"product":3, "count":1}, 
   {"product":4, "count":1},{"product":5, "count":1}
 ] 

I tried the following code, but it is not working.

 {
        $project: {
          _id: 0,
          list: { $setEquals: ["$postType", "buy"] }
        }
      }

I just inserted 4 products but it will be 10 actually.

1
  • Grouping in MongoDB usually involves the $group stage. The documentation contains a number of examples, this one in particular may be relevant for your situation. Should help you get started Commented Oct 24, 2022 at 18:48

1 Answer 1

1

Would be this one:

db.collection.aggregate([
  { $match: { postType: "buy" } }, // filter on postType = "buy"
  {
    $group: {                      // group and count
      _id: "$product",
      count: { $count: {} }
    }
  },
  {
    $project: {                   // some cosmetic
      product: "$_id",
      count: 1,
      _id: 0
    }
  }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you , let me try
Is there any option to populate product collection data based on the product id ? product id will be mongodb object id.
Not clear what you mean, sorry.
Is it possible to count other postType in the same query? like my expected output now is [{"product":1, "count":2, "shareButtonCount":4, viewCount":4},{"product":2, "count":3, shareButtonCount":4, viewCount":4},{"product":3, "count":1, shareButtonCount":4, viewCount":4}, {"product":4, "count":1, shareButtonCount":4, viewCount":4},{"product":5, "count":1, shareButtonCount":2, viewCount":4} ]
No idea what you mean. Perhaps replace _id: "$product" by _id: {product: "$product", postType: "$postType"} and skip the $match. Maybe open a new question for it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.