$bucket functionality missing from DocumentDB

Hello,
It seems that $bucket aggregation functionality (https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/#mongodb-pipeline-pipe.-bucket) is missing from the node driver.
Is there a specific reason for this, or a plan to include it?
Can anyone suggest a workaround for $bucket aggregation using node?

I had assumed the node driver would support all features of the mongo client.
Thanks

Hi @Mike_Monteith and welcome in the MongoDB Community :muscle: !

I built a little test using these 2 pages:

const { MongoClient } = require("mongodb");

const uri = "mongodb://localhost";

const client = new MongoClient(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

async function run() {
  try {
    await client.connect();

    const db = client.db('test');
    const artists = db.collection('artists');

    await artists.drop();

    await artists.insertMany([
      { "_id" : 1, "last_name" : "Bernard", "first_name" : "Emil", "year_born" : 1868, "year_died" : 1941, "nationality" : "France" },
      { "_id" : 2, "last_name" : "Rippl-Ronai", "first_name" : "Joszef", "year_born" : 1861, "year_died" : 1927, "nationality" : "Hungary" },
      { "_id" : 3, "last_name" : "Ostroumova", "first_name" : "Anna", "year_born" : 1871, "year_died" : 1955, "nationality" : "Russia" },
      { "_id" : 4, "last_name" : "Van Gogh", "first_name" : "Vincent", "year_born" : 1853, "year_died" : 1890, "nationality" : "Holland" },
      { "_id" : 5, "last_name" : "Maurer", "first_name" : "Alfred", "year_born" : 1868, "year_died" : 1932, "nationality" : "USA" },
      { "_id" : 6, "last_name" : "Munch", "first_name" : "Edvard", "year_born" : 1863, "year_died" : 1944, "nationality" : "Norway" },
      { "_id" : 7, "last_name" : "Redon", "first_name" : "Odilon", "year_born" : 1840, "year_died" : 1916, "nationality" : "France" },
      { "_id" : 8, "last_name" : "Diriks", "first_name" : "Edvard", "year_born" : 1855, "year_died" : 1930, "nationality" : "Norway" }
    ]);

    const result = await artists.aggregate( [
      {
        $bucket: {
          groupBy: "$year_born",                        // Field to group by
          boundaries: [ 1840, 1850, 1860, 1870, 1880 ], // Boundaries for the buckets
          default: "Other",                             // Bucket id for documents which do not fall into a bucket
          output: {                                     // Output for each bucket
            "count": { $sum: 1 },
            "artists" :
              {
                $push: {
                  "name": { $concat: [ "$first_name", " ", "$last_name"] },
                  "year_born": "$year_born"
                }
              }
          }
        }
      },
      {
        $match: { count: {$gt: 3} }
      }
    ] ).toArray();

    console.log(result);
  } finally {
    await client.close();
  }
}
run().catch(console.dir);

And I get this result:

[
  {
    _id: 1860,
    count: 4,
    artists: [ [Object], [Object], [Object], [Object] ]
  }
]

To run this I did:

mkdir nodemdb
cd nodemdb
npm init -y
npm install mongodb
vim index.js // put the above code in this file
node index.js

It works fine by me :smile:.
I hope this helps.

Cheers,
Maxime.

You’re right. Thank you Maxime.

My problem is actually that Microsoft Azure’s weird fake mongo product doesn’t support $bucket :sob:

No news here :-).

That’s why we are running compatibility tests on DocumentDB and CosmosDB and they are both failing hard.

Here are the latest results I have for CosmosDB.

Also, just saying… But when we run these tests on Atlas it costs us less than $1 for a cluster for 30min. It’s more about $0.50 in reality as an M30 cluster costs $0.59/hour and I just need 30min. But on CosmosDB, it costs us more than $200 for a single run… Not joking :confused:.

Cheers,
Maxime.

1 Like

I wish I had the power to choose which cloud provider we use at work :cry:
It would not be Azure

1 Like

MongoDB Atlas is also available on Azure. You don’t even have to sell them a new Cloud Provider :smiley: !

The big difference is that MongoDB Atlas provides the REAL MongoDB on Azure…

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.