Why mongorestore always returning the error that directory not found even with correct file path? - mongodb

I want to import some data in mongoDb database's collection. That collection already have some data and i want to import some more data into it. So for that i am using this command
mongorestore --drop -d local-db -c users /Documents/cwc/mongo-backups/Users.bson
but this command is always returning this error
Failed: mongorestore target '/Documents/cwc/mongo-backups/Users.bson' invalid: stat /Documents/cwc/mongo-backups/Users.bson: no such file or directory
How can i fix this?

Related

MongoDB Failed: can't create ActualPath object from path dump: stat dump: no such file or directory

I have a bunch of mongo databases that I need to restore. I used mongodump to get the backup directories, which include the collections inside of them. Something like this:
|- mydir
|-- db1
|--- collection1
|--- collections2
|-- db2
|--- collection1
|--- collections2
I cd into mydir and do mongorestore and I get the following error:
2016-07-25T10:41:12.378-0400 using default 'dump' directory
2016-07-25T10:41:12.378-0400 Failed: can't create ActualPath object from path dump: stat dump: no such file or directory
Then I try to restore a specific database like this: mongorestore db2 and get the following errors:
2016-07-25T10:47:04.413-0400 building a list of dbs and collections to restore from db2 dir
2016-07-25T10:47:04.413-0400 don't know what to do with file "db2/collection1.bson", skipping...
2016-07-25T10:47:04.413-0400 don't know what to do with file "db2/collection1.metadata.json", skipping..."db2/collection2.bson", skipping...
2016-07-25T10:47:04.413-0400 don't know what to do with file "db2/collection2.metadata.json", skipping...
2016-07-25T10:47:04.414-0400 done
No matter what I do or what I try, I alternate between these two errors. And it's the same for any of the databases I use.
I tried using the --db flag, the -d parameter, setting the dump path as the third argument (mongorestore --db [db] [dump_path]). Everything I found around Stackoverflow. Nothing.
I'm stuck on this and I have no idea how to proceed.
EDIT
OS: Ubuntu 14.04
Installed MongoDB following this guide:
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
mongorestore with no dump_path positional argument is expecting there to be a folder named dump in your current working directory. If the folder dump does not exist, you will get the "can't create ActualPath..." error.
So basically if you do not have a folder named dump, you will need to pass that positional argument. So from the parent directory of mydir run:
mongorestore mydir
If you want to use the --db option you will need to specify the dump path all the way down to the directory that contains the .bson files for that database or a specific .bson file.
So for example to restore all collections for db1:
mongorestore --db db1 ./db1
Or to just restore collection1:
mongorestore --db db1 ./db1/collection1.bson

Cannot import example dataset (the system cannot find the specified file)

I am following the example given on MongoDB's website here, but I am running into trouble when trying to import sample data.
When running the command
mongoimport --db test --collection restaurants --drop --file primer-dataset.json
I get the error:
Failed: open primer-dataset.json: The system cannot find the file specified
The problem is, I am not sure what directory MongoDB expects this file to be in. I tried placing it in data/db, but that did not work. Note that I am only using default settings.
I know this is a somewhat trivial question and I feel stupid for asking it but I can not find documentation on this anywhere. Where is MongoDB expecting import files?
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.
For Windows!
Save file using notepad++ in .json format at MongoDB/bin where mongoimport command is present.
Simple notepad has trouble doing it.
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.
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.
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;
In my case, I removed the --drop parameter and it worked perfectly. I guess, it is throwing this error:
Failed: open paht/file-name.json: The system cannot find the file specified.
because the collection it wants to drop is not available, because I have not created any before.
you must copy your json file into C:\Windows\System32 and write this command on cmd:
mongoimport --db test --collection mongotest --type json --file yournamefile.json

Inserting JSON document into mongo error

I'm trying to insert the TWDS1E1.json file into mongodb through the command prompt:
db.collections.insert( TWDS1E1.json )
But getting the error:
TWDS1E1.json is not defined.
Mongo is not my thing, what am I doing wrong here?
In command prompt whose directory path is the path where mongoimport.exe is available type the commands
For normal JSON
mongoimport -d test -c docs --file example2.json
For array type JSON
mongoimport --jsonArray -d test -c docs --file example2.json
Please see docs for more information
You cannot use the collection.insert() command to insert a file.
insert() is used to insert actual objects, e.g.
db.myCollection.insert({"name":"buzz"});
To bulk load a JSON file, use mongoimport

Mongodb monogorestore "root directory must be a dump of a single database"

I'm trying to restore a mongodump to a differently named database (which should be possible via --db <dbname> switch).
My working directory contains a single dump folder, which contains a single db dump.
However, when I try this command:
mongorestore --port xxxxx --db some_destination_db
I get the following error:
ERROR: ERROR: root directory must be a dump of a single database
ERROR: when specifying a db name with --db
I have no idea why I'm getting this, and can't find any help on google. Anyone have any ideas?
You need to include the path of the source db dump directory to restore from (as at MongoDB 2.6).
For example, if restoring from "twitter" to "some_destination_db" your command line would be similar to:
mongorestore --port xxxxx --db some_destination_db dump/twitter
May be your destination path contains some subfolders

Mongo DB and inserting bson files into a database

I was given a data dump of bson files. In the mongo db, the database and the collections exists. These are updates to each of the collections in the database. So, in the given directory, there are about 30 bson files for each collection.
From the command line, I am using ubuntu, how do I append and load? Mongo is on my localhost with no username or password.
Thanks
Took me a while to get around this excuse for an error. In the end I went to the directory outside of my dump folder, and did the following...
For a full DB restore:
mongorestore --drop dump/mydb
Note that the mongodump operation will create individual folders for each database within the dump folder it creates, so you need to specify the full relative path, as above.
For a single collection:
mongorestore --drop -d mydb -c mycollection dump/mydb/mycollection.bson
The usual syntax is:
mongorestore -d dbname -c collectionname dir/file.bson
are you looking for mongorestore? http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore
import Bson
mongorestore -d dbname -c collectionname dir/file.bson
import Json
mongoimport --collection NAME --file NAME.
http://docs.mongodb.org/v2.2/reference/mongoimport/
Since Mongo restore does not update the current records this would not be a good choice.
Mongorestore only appends new records as stated:
mongorestore just does inserts with the data to restore; if existing
data (like with the same _id) is there it will not be replaced.
You may wish to build a BSON parser in your language of choice and make a more complex tool than mongorestore, since mongorestore is only designed to "restore" (as the name kinda suggests) a database/collection you will need to write something a little more complicated to do what you want and that depends heavily on your server-side language.
Edit
This is actually better done with mongoexport and mongoimport:
http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongoimport
With mongoexport you could export a JSON file and give the command line for it do upserts. So I would personally go back to the person who gave this file and tell them that you actually want a mongo export file instead.
1) Go to the directory where the "dump" folder is located in CMD.
2) Run the mongorestore command.