mongoimport json 文件语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35153774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:15:50  来源:igfitidea点击:

mongoimport json file syntax

jsonmongodbmongoimport

提问by Archana Mundaye

I'm trying to import json document using mongoimport command and have tried all possible ways but it's giving be below error. Plz tell me what m I doing wrong here. I've given full path of json doc. I want to import big document having 800+ records but because it failing so currently my students.json contains simple one line {name : "Archana"} but even this is failing.

我正在尝试使用 mongoimport 命令导入 json 文档并尝试了所有可能的方法,但它给出了以下错误。请告诉我我在这里做错了什么。我已经给出了 json 文档的完整路径。我想导入具有 800 多条记录的大文档,但因为它失败了,所以目前我的 Students.json 包含简单的一行 {name : "Archana"} 但即使这样也失败了。

C:\data\db>mongo
2016-02-02T17:48:44.788+0530 I CONTROL  [main] Hotfix KB2731284 or later update is installed, no need to zero-out data
MongoDB shell version: 3.2.1
connecting to: test
> show collections
names
students
> mondoimport -d test -c students students.json
2016-02-02T17:50:21.001+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -d test -c students < students.json
2016-02-02T17:50:25.840+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -d test -c students < file students.json
2016-02-02T17:50:31.233+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -d test -c students < f students.json
2016-02-02T17:50:35.417+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -d test -c students < f C:\data\db\students.json
2016-02-02T17:50:51.658+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -d test -c students < f "C:\data\db\students.json"
2016-02-02T17:50:56.897+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -d test -c students C:\data\db\students.json
2016-02-02T17:51:06.849+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:15

> mondoimport -db test -c students C:\data\db\students.json
2016-02-02T17:54:33.545+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:16

> mondoimport --db test --collection students C:\data\db\students.json
2016-02-02T17:56:24.253+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:14

> mondoimport --db test --collection students --file C:\data\db\students.json
2016-02-02T17:56:33.589+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:14

> mondoimport --db test --collection students --file C:\data\db\students.json --jsonArray
2016-02-02T17:58:21.131+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:14

> mondoimport --db test --collection students C:\data\db\students.json --jsonArray
2016-02-02T17:58:32.650+0530 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:14

回答by Akshay Seth

You are probably running this command under mongo shell. As you can see in the path program files/mongodb/server/3.2/binthere is no command under mongod as mongoimport. So in order to solve the problem, navigate your cmd prompt to the file and without using mongo shell(mongo)run the command in cmd prompt.

您可能正在 mongo shell 下运行此命令。正如您在路径中看到的那样,program files/mongodb/server/3.2/binmongod 下没有命令 mongoimport。因此,为了解决该问题,请将您的 cmd 提示符导航到该文件,而不使用mongo shell(mongo)在 cmd 提示符下运行该命令。

回答by Archana Mundaye

I was trying to use mongoimport from mongo shell. So answer is "mongoimport is different utility" so if you want to use mongoimport then after starting mongo server using "mongod", use need to start different cmd and go to C:\Program Files\MongoDB\Server\3.2\bin or it should be in ur path and then u need to run "mongoimport" command. If you start using "mongoimport" from "mongo" shell then it will not work.

我试图从 mongo shell 使用 mongoimport。所以答案是“mongoimport 是不同的实用程序”所以如果你想使用 mongoimport 那么在使用“mongod”启动 mongo 服务器后,使用需要启动不同的 cmd 并转到 C:\Program Files\MongoDB\Server\3.2\bin 或它应该在你的路径中,然后你需要运行“mongoimport”命令。如果您从“mongo”shell 开始使用“mongoimport”,那么它将不起作用。

回答by Archana Mundaye

mongoimport is different utility so use different command prompt.

mongoimport 是不同的实用程序,因此使用不同的命令提示符。

回答by Piyush Singh

mongoimport command

mongoimport 命令

Try the following steps in the given manner:

按照给定的方式尝试以下步骤:

Step1. On new command prompt, cd to location of mongo's bin directory. for example:

第1步。在新的命令提示符下, cd 到 mongo 的 bin 目录的位置。例如:

cd c:\MongoDB\bin

Step2. Run the following command:

第2步。运行以下命令:

mongoimport --db test --collection restaurants --file location/of/jsonfile

回答by Deepak

Hi you need to do the following things:

您好,您需要做以下几件事:

  1. Save the JSON file in a directory, e.g.: C:/Users/DEEPU/students.json
  2. Open a command prompt and go to that directory by using the cdcommand where your file is saved.
  3. Then type mongoimport --drop -d students -c grades grades.json
  1. 将 JSON 文件保存在一个目录中,例如: C:/Users/DEEPU/students.json
  2. 打开命令提示符并使用cd保存文件的命令转到该目录。
  3. 然后输入 mongoimport --drop -d students -c grades grades.json

Click the below linkto see it working.

单击下面的链接以查看它的工作原理。

回答by Raj

try like this. copy past this code in text document and save it with .sh extension. place both the files on desktop and execute .sh file in terminal

像这样尝试。将此代码复制到文本文档中并使用 .sh 扩展名保存。将两个文件放在桌面上并在终端中执行 .sh 文件

mongoimport --db test --collection students --file students.json

回答by Soshiv Upreti

bin>mongoexport -d databasename -c collectionName -o export.json

bin>mongoexport -d 数据库名称 -c 集合名称 -o export.json

回答by codeboy

The same works for mongorestoretoo. If you need to restore a db or its schema you need to go to the path where the .zip file is and then run the mongorestore command. mongorestore <filename>

这同样适用mongorestore。如果您需要恢复数据库或其架构,您需要转到 .zip 文件所在的路径,然后运行 ​​mongorestore 命令。mongorestore <filename>

回答by Vinod Poorma

if you are using windows machine then,open new command prompt and try like this:

如果您使用的是 Windows 机器,请打开新的命令提示符并尝试如下操作:

mongoimport.exe -h localhost:27017 -d sampleTestDB -c testc -u "" -p "" D:\mongoSample.json

Note: if you not set the PATH, then set the path and then try this.

注意:如果你没有设置PATH,那么设置路径然后试试这个。

how to set the path?

如何设置路径?

==> in Environment Variables, select PATH then click on "edit" copy your mongodb installed path as shown here and save it

==> 在 Environment Variables 中,选择 PATH 然后单击“编辑”复制您的 mongodb 安装路径,如图所示并保存

C:\Program Files\MongoDB\Server.4\bin

回答by Mukesh Kumar Gupta

I was also facing the same problem on my ubuntu 16.04. I misunderstood, I was running inside MongoDB shell. mongoimport is utility tool. You just need to open new terminal and run mongoimport command not inside the mongodb shell. mongoimport --db <dbname> --collection zips --drop --file <location of file>

我在 ubuntu 16.04 上也遇到了同样的问题。我误解了,我在 MongoDB shell 中运行。mongoimport 是实用工具。您只需要打开新终端并在 mongodb shell 中运行 mongoimport 命令。 mongoimport --db <dbname> --collection zips --drop --file <location of file>