Breaking

Code Gripper
Responsive Advertisement
  • Home
Homeaggregate

How to efficiently count filtered documents in MongoDB $group operator

byDev3 -January 13, 2023
0

Efficient Way would be

Filters down the documents by using $match to pass only matching documents to the next pipeline. By placing $match at the very beginning of a pipeline, the query can take advantage of indexes.

Use $project to pass along the documents with only the required fields to the next stage in the pipeline, this will further reduce data to the next pipeline.

db.getCollection('collection').aggregate([ 
  { $match: {"timestring":{$gte:"20230112000000"}} },
  { $project: {"timestring":1, "campaign_id":1}},
{ $group : { _id : "$campaign_id", count : { $sum : 1 } } },
{ $sort : { "count" : -1 } } ])
Tags aggregate MongoDB
  • Facebook
  • Twitter

You may like these posts

Post a Comment

Previous Post Next Post

Follow Us

Popular Posts

Subscribe Us

Facebook

Categories

Main Tags

Code Gripper

September 30, 2023

Total Pageviews

Search This Blog

CODE GRIPPER

Code Gripper
CODE GRIPPER

  • Home
  • Python
  • PHP

Menu Footer Widget

  • Home
  • About
  • Contact Us
Code Gripper

About Us

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's.

Categories

Main Tags

Latest Posts

Most Popular

Design by Blogger | Distributed by Gooyaabi
  • Home
  • About Us
  • Contact Us
  • RTL Version

Contact Form