Syntax error: missingl before statement while outputting MongoDB query to a text file

柔情痞子 提交于 2019-12-25 02:15:08

问题


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:


--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


来源:https://stackoverflow.com/questions/22540405/syntax-error-missingl-before-statement-while-outputting-mongodb-query-to-a-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!