4

I have a Node app interacting with a Mongo database. When running on the old setup or locally on MongoDB 4.4.0

When I deploy to AWS Document DB 3.6.0 and try to run a query with the below $group clause, I get an error that Feature not supported: $$ROOT

  {
    $group: {
      "_id": "$user_id",
      "total_paid_amount": {
        $sum: "$amount"
      },
      "data": {
        $push: "$$ROOT"
      }
    }
  }

According to this doc https://docs.aws.amazon.com/documentdb/latest/developerguide/developerguide.pdf, $$ROOT is not supported in DocumentDB

How can I work around this issue, or perform the same query without pushing $$ROOT?

Thanks in advance!

Joshua Ohana
  • 5,613
  • 12
  • 56
  • 112

2 Answers2

2

Ended up just having to push the items I actually needed/wanted instead of all via $$ROOT

$push: {
  fieldOne: "$fieldOne",
  fieldTwo: "$fieldTwo"
}
Joshua Ohana
  • 5,613
  • 12
  • 56
  • 112
  • Thank you very much for this suggestion. You saved my day. I ended up doing something like this using Mongoose: ``` objects: { // $push: "$$ROOT" $push: (["_id", "__v", ...Object.keys(MongooseObject.schema.obj)].reduce((accumulator, currentValue) => ({ ...accumulator, [currentValue] : `$${currentValue}` }), {})) } ``` – zubozrout May 05 '21 at 11:44
  • For people who have too much data to use `$push`, it's not integral to the answer; I added $$ROOT to a $group aggregation with `"$group": { ..."doc": { "$first": { fieldOne: "$fieldOne", fieldTwo: "$fieldTwo"}}}`. – Noumenon Jun 18 '21 at 17:02
0

Support for $$ROOT was added to Amazon DocumentDb in October 2021.

tmcallaghan
  • 1,292
  • 2
  • 10
  • 20