Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GithubArray opened this issue Sep 6, 2023 · 2 comments

Comments

@GithubArray
Copy link
Contributor

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?

@feliixx
Copy link
Owner

feliixx commented Sep 23, 2023

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.

@GithubArray
Copy link
Contributor Author

Just to note a workaround, we can select no document by _id: {$exists: false} and then put the $documents stage in an $unionWith pipeline as a workaround in playground.

db.collection.aggregate([
  {
    "$match": {
      _id: {
        "$exists": false
      }
    }
  },
  {
    "$unionWith": {
      "coll": "collection",
      "pipeline": [
        {
          "$documents": [
            {
              "hello": "world"
            }
          ]
        }
      ]
    }
  }
])

Mongo Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants