How to import form Json file to MongoDb - json

How to import a json file to mongodb?
I tried:
mongoimport --db test --collection restaurants --drop --file primer-dataset.json

mongoimport is a stand-alone application that needs to be executed from the shell, (Window Command Prompt, Bash, etc). It seems you are currently executing the code inside the Mongo shell itself.

The docs read:
The mongoimport tool imports content from an Extended JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.
The word "potentially" means it should be a valid json with expected structure. Not an arbitrary json.

Related

Import many JSON files into MongoDB with mongoimport

I use Linux, and in a local folder I have myriad of files (with no extension, but they are JSON array files).
I would like to import all of them into the database (coches) and collection (dtc) created in mongo.
I tried several codes found in different questions (1, 2) with no success. This is a list of files found in my local folder, but there are many more.
/media/mario/prueba/2020-02-25_00-30-33_588844_0Auy
/media/mario/prueba/2020-02-25_06-26-02_816819_KXbU
/media/mario/prueba/2020-02-25_07-07-22_868748_DCmL
/media/mario/prueba/2020-02-25_16-02-12_371020_eYjf
This is an example I tried unsuccessfully:
for filename in *; do mongoimport -db coches --collection dtc --file "$filename" done;
I am new no Mongo and would like to know how to deal with this situation.
How can I import plenty of JSON files (unextensioned) to the database coches and collection dtc?

How to export subset of data from mongodb

I currently have a large database, and I need a means of backing up subsets of the data that can then be imported on another mongodb instance.
For example, I would need to find all documents that contain a key, so essentially: find({key: 'somekey'}), and then export that data set. I thought to simply run the query in NodeJS, and save the data in JSON format. I don't think this is optimal as through my understanding simply importing the JSON data again (if needed in the future) won't be a straightforward task as the data-types will be lost.
So my question is, how would I go about exporting a subset of the dataset so that it may be possibly re-imported into another mongodb instance on another server.
Thanks to #Veeram's comment, the way to do this is as BSON so that it retains all the data structure:
sudo mongodump -d DB_Name -c Collection -q '{"key_name": "value"}' --out /home/collection
Then to import it back:
sudo mongorestore -d DB_Name -c Collection /home/collection/DB_Name/Collection.bson

Getting RECORD Array in MongoDB

I imported json data from mysql using this command :
mongoimport --db your --collection categories categories.json --type json
But when i stared search data i found an issue that mongodb collection have RECORDS Array and not imported ids as object like first one.
Any one know ? how to import data from mysql to mongodb that will be as Object not an extra RECORDS Array ?
I think RECORDS is coming from your mysql, please check your JSON file by opening it in editor like sublime.
Your Answer to export from mysql to mongo with JSON Objects:
Install Gem :
gem install mysql2xxxx
Then run :
mysql2json --user=root --database=yourdb --execute "select * from categories" > cat.json
So after run above command you will get clean records in json format, don't know how you are importing but i don't think RECORDS should come.
After done this you can use :
mongoimport --db your --collection categories cat.json --type json
Hopefully this will work.

mongoimport removes first record

On CSV import - mongo is removing the first record. Any ideas why?
mongoimport --file abilities.csv --type csv --db talent --headerline --collection abilities
I checked and it just didn't import in order. For some reason, the document was on a different list page. All worked out! Thanks for your help.

Mongoimport without mongoexport

Suppose I have ssh access to a server with mongodb on it. However, suppose the server does not have mongoexport installed, and I cannot install it. I can use mongo, interactively or feed it a script. I wish to export a subset of the data and import it on my local computer. Ideally, I'd like to run a script or command that saves the data in the same format as mongoexport, so I can import it with mongoimport locally. https://stackoverflow.com/a/12830385/513038 doesn't work (as it has extra line breaks in the results), nor does using printjsononeline instead, because some values get printed differently and I end up with "Bad characters" and "expecting number" errors when I run mongoimport.
Any ideas? Again, I'd like to use mongoimport if possible, but other sufficiently workable ways are acceptable, as well.