Syntax error: missingl before statement while outp

2019-08-19 07:33发布

I am 1 day old to MongoDB so bear with me. I am simply trying to output a simple query to a *.json file. The query is:

db.collection.findOne() // collection is substituted with the correct collection name, the query preludes 'use db' command where db is substituted with correct db name

Then, in reference to this article and a bunch of other SO answers, I perform the following query:

--eval "printjson(db.results.findOne())" >> sample.json

I even created a blank sample.json file, and I experimented --eval with -eval etc. I keep getting:

SyntaxError: missing ;before statement (shell):1

I add semi colons at arbitrary places, but I have no idea why this doesn't work for me.

Can anybody point out what I am missing here?

Complete sequence of commands:

mongo
use dbname
--eval "printjson(db.collectionName.findOne())" >> sample.json

P.S: The command db.collectionName.findOne() does give me an output

1条回答
时光不老,我们不散
2楼-- · 2019-08-19 07:42

--eval should be passed as an argument to "mongo" command. I tried below and it worked:

C:\>mongo localhost:27017/dbname --eval "printjson(db.results.findOne())" >> sample.json
查看更多
登录 后发表回答