問題描述
無法導入示例數據集(系統找不到指定文件) (Cannot import example dataset (the system cannot find the specified file))
我正在遵循 MongoDB 網站 這裡 上給出的示例,但我在嘗試導入示例數據時遇到了麻煩。
運行命令時
mongoimport ‑‑db test ‑‑collection restaurants ‑‑drop ‑‑file primer‑dataset.json
我收到錯誤:
失敗:打開Primer‑dataset.json: 系統找不到指定的文件
問題是,我不確定MongoDB希望這個文件在哪個目錄。我試著把它放在data/db ,但這沒有用。請注意,我只使用默認設置。
我知道這是一個微不足道的問題,我覺得問這個問題很愚蠢,但我在任何地方都找不到關於此的文檔。
參考解法
方法 1:
MongoDB expects the file to be in the directory from where you are running the command mongoimport.
If you place your file under data/db then set mongodb path as global environment variable and execute the command from data/db directory.
Additionally if you have security enabled for your mongodb then you need to execute command as below
mongoimport ‑‑username admin ‑‑password password ‑‑db test ‑‑collection restaurants ‑‑drop ‑‑file primer‑dataset.json
here admin is the user authorized to perform db operations for test database and restaurants is the collection name.
方法 2:
For Windows! Save file using notepad++ in .json format at MongoDB/bin where mongoimport command is present.
Simple notepad has trouble doing it.
方法 3:
It happened to me as well. The issue was that though the file was visible as restaurants.json actually the file was restaurants.json.json (since saved in JSON format). The issue was resolved after properly changing the name.
方法 4:
i have trouble like you, check you path to file, mongoimport.exe and your file may be stay in another folders.
use mongoimport ‑d test1 ‑c restaraunts companies.json
for import to mongodb.
方法 5:
Check the filename extension, and make sure it's a ".json" file; After this I successfully run the
mongoimport ‑‑db test ‑‑collection restaurants ‑‑drop ‑‑file [path\to\Json file]
command;
(by ericgrosse、rajadilipkolli、code_love、Amjadh_Ifthikar、Uladz Kha、Anqi Bai)