craigmiller160
craigmiller160

Reputation: 6273

Mongo DB Shell - Multi-Line query not working

I'm running version 3.6 of Mongo DB, and I'm using the mongo shell. I'm running it on MacOS Sierra. I'm having a lot of grief with this multi-line query. I'm copying and pasting it from Sublime Text to the Mongo Shell.

Ok, so here is the query:

db.submissions.aggregate([
    {
        "$match": {
            "FieldValues.Value": "Craig"
        }
    },
    {
        "$lookup": {
            "from": "aliases",
            "localField": "FieldValues.Name",
            "foreignField": "Fields.Field",
            "as": "R"
        }
    },
    {
        "$unwind": "$R"
    },
    {
        "$unwind": "$R.Fields"
    },
    {
        "$unwind": "$FieldValues"
    }
])

So, let's run through the combinations of when I get errors, and when I don't

1) Run the query as-is, this is what happens in the shell:

> db.submissions.aggregate([
... {
... "$match": {
... "FieldValues.Value": "Craig"
... }
... },
... {
... "$lookup": {
... "from": "aliases",
... "localField": "FieldValues.Name",
... "foreignField": "Fields.Field",
... "as": "R"
... }
... },
... {
... "$unwind": "$R"
... },
... {
...     "$unwind": "$R.Fields"
...   },
...   {
...   
Display all 172 possibilities? (y or n)
...   wind": "$FieldValues"
2017-11-27T09:46:39.650-0500 E QUERY    [thread1] SyntaxError: missing : after property id @(shell):22:6
>   }
2017-11-27T09:46:39.652-0500 E QUERY    [thread1] SyntaxError: expected expression, got '}' @(shell):1:0
> ])

I should point out that last line is my current "entry point" (I can't think of a better name for it), and my curser is at the end of the "])".

2) Run the query, just without that last "$unwind".

Everything works.

3) Run the query with the last "$unwind" on one line.

This means:

{"$unwind": "$FieldValues"}

Then, everything works.

Conclusion/Question

So, WTF? Why is Mongo Shell freaking out when I paste this query in pretty-printed, with that last "$unwind"?

I should also point out it is NOT the "$unwind" itself that causes the problem. Any additional query that I put into the pipeline there causes a similar error when I paste it in.

Upvotes: 2

Views: 3403

Answers (1)

kevinadi
kevinadi

Reputation: 13795

This reaction from the mongo shell:

Display all 172 possibilities? (y or n)

seems to hint that you have a tab character in your copied text.

Remove the tab characters by using space for indentation instead of tabs.

Upvotes: 1

Related Questions