mongoplayground icon indicating copy to clipboard operation
mongoplayground copied to clipboard

$documents - query failed: (InvalidNamespace) '$documents' can only be run with {aggregate: 1}

Open GithubArray opened this issue 2 years ago • 2 comments
trafficstars

When I try to use $documents operator in playground,

db.collection.aggregate([
  {
    "$documents": [
      {
        "test": 123
      }
    ]
  }
])

I get below error:

query failed: (InvalidNamespace) '$documents' can only be run with {aggregate: 1}

Playground to reproduce.


Alternatives that I have tried(but also failed): Removing collection in db.collection.aggregate([...]): will get syntax error like:

Invalid query:

Line 1: Expected '.' instead of '('

Playground

What could be a correct way to use $documents in Mongo Playground?

GithubArray avatar Sep 06 '23 02:09 GithubArray

Hi @GithubArray,

Thanks for the report. This is happening because $documents is only available when calling db.aggregate() (ie, not running aggregation on a specific collection). This is not supported by the playground yet and might be tricky to implement, so the best solution for now is to remove the $documents stage, put the documents in an other collection, and run the aggregation on this collection.

feliixx avatar Sep 23 '23 10:09 feliixx