How do I export the Collection from Mongo DB to xsl file.
I am using MongoVue export, but it is just displaying the Document and Number of keys in it, but not all the Key and Value pairs in the document..
Please help how to export a mongodb collection to xsl.
I'm not sure of using MongoVue (I haven't used that before) but if you are ok with an extra manual step you could always use mongoexport to output to a CSV file. You can then open and save as XSL via excel.
Here's the syntax for using mongoexport:
mongoexport -d <database> -c <collection> --csv --fields <field1,field2,...> -o <filename>
This will use the localhost mongo instance by defaykt so if you need to connect to another server see the docs:
http://docs.mongodb.org/manual/reference/program/mongoexport/
Related
Using mongodb and am trying to get a specific value from a collection in the db. I am able to get the complete export using
mongoexport --db database --collection name
But the output is a large file and I am trying to get a specific set of key/pair in it.
ex: "Name": "Value"
there are several names and I just need to print all the names in the collection.
What would be the command syntax from a UNIX shell ?
I looked at this but that is from with in the mongo shell.
thanks
To request all fields from collection yourCollection in MyDatabase :
mongo --quiet 127.0.0.1/MyDatabase --eval 'printjson(db.yourCollection.find().toArray());'
To request only fields name field from collection yourCollection in MyDatabase :
mongo --quiet 127.0.0.1/MyDatabase --eval 'printjson(db.yourCollection.find({},{"_id":0,"name":1}).toArray());'
You could also have a script to execute and save you the time of manually writing in those commands. Execute something like
mongo localhost:27017/test myfile.js
and in the javascript file input
db.name.findOne()
db.name().find({},{"_id":0,"Name":1}).toArray());
please see https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/ and [How to execute mongo commands through shell scripts?
If your goal is to export documents matching a specific condition from the database, you can pass a query to mongoexport using the -q parameter. For example:
mongoexport -d db -c coll -q '{"Name":"Value"}'
This will export all documents containing the field "Name" having the value "Value".
You can also pass the --quiet parameter to mongoexport if you prefer to have the output without any informative content, such as number of exported documents.
Please see https://docs.mongodb.com/manual/reference/program/mongoexport/ for more information regarding the mongoexport tool.
I have below in a shell script to export certain fields from a mongo collection to a CSV file.
mongoexport --host localhost --db mydb --collection ratings --csv > data.csv --fields userId,filmId,score
My problem is that the result is generated comes with the header values.
ex:
userId,filmId,score
517,533,5
518,534,5
Is there a way that I can generate the csv file with out the header fields?
The mongoexport utility is very spartan and does not support a load of features. Instead the intention is that you augment with other available OS commands or if you really must then create your own code for explicit needs.
But this sample using tail is quite simple to skip the first emitted header line when you consider that all output is going to STDOUT by default anyway:
mongoexport --host localhost --db mydb --collection ratings \
--fields userId,filmId,score \
| tail -n+2 > data.csv
So it is just "piping through" | the tail command with the -n+2 option, that basically says "skip the first line" and then you just redirect > output to the file you want.
Just like most command line utilities, there is no need to build in options that can be performed with other common utilities in such a chained pattern as above. That is why there is no such option built in.
Since version 3.4 you can add --noHeaderLine as option within the command.
I have a file named services.json containing a data base that I exported from a windows mongodb, and I want to import that file into robomongo (connected to mongodb installed by npm) on Ubuntu.
I'm a beginner and I don't know how to proceed, which terminal use (robomongo or Ubuntu)?
to import data for a collection in Robomongo:
Right click on collection.
Select 'insert Document'.
Paste your json data
Click validate.
Click save.
Ok, I found the answer. In shell Mac OS X or Unix type:
$ mongoimport -d your Database Name -c your Collection Name --file /path/to/my/fileThatIwantToImport.json
For anyone wishing to use mongoimport with a remote db (#andi-giga), here's what I did to make it work:
mongoimport -h xxx.mlab.com --port 2700 -d db_name -c collection_name -u user_name -p password --type json --file /Path/to/file.json
Arguments should be self-explanatory.
-h hostname
More information at this link
I don't have enough points to comment on Varun's answer, but if you use export jsonArray and then import using Robo3T (Robomongo), make sure to remove the commas in between the objects, as well as remove the square brackets.
It's not really a JSON format that ROBO 3T accepts, but rather bunch of JSON objects separated by newlines.
(if you use export Standard, then it's already formatted for document insert)
if this is not a bson, and only json, you can use mongoimport --jsonArray . refference Insert json file into mongodb
RoboMongo is just the UI for your mongod which is the primary daemon process for the MongoDB system.
The only option to import from RoboMongo is
Right Click on Collection -> Insert Document
Apart from this you can import using the mongoimport command from terminal.
Open terminal and type mongo
Now in mongo interactive shell
Use the following command to import the json file as collection
mongoimport -d database_name -c collection_name --file < path to the
json file
Tested :
mongoimport --jsonArray -d <DataBase Name> -c <Collection Name> --file /path/to/my/fileThatIwantToImport.json
It works very well!
Insert Document will insert all the JSON file data under a single document.
Apparently the tool does not support JSON import.
There are two ways to import the database into MongoDB. one is with robomongo/Robo 3T and one is with a shell command. I always choose the second method due to fewer and easy steps.
FIRST METHOD
Install MongoDB on your machine. Also, check it was installed properly or not by using mongod command on your terminal. So, for importing a new DB on your MongoDB write this command on your terminal
mongostore -host <HostIp | 127.0.0.1> -port <mongoPort | 27017> -db <DBname> <Directory-path>
So, for example you’re running MongoDB on local machine with default port i.e 27017 and your DB files are store at /usr/library/userDatabase then write this command and check DB is imported in your MongoDB
mongostore -host 127.0.0.1 -port 27017 -db userDatabase /usr/library/userDatabase
For more details check this article.
Import MongoDB using shell and robomongo/Robo 3T
How can I dump an entire MongoDB database as text (plain text, json, or CSV)?
I'm using an application I'm not too familiar with. I'd like to
clear the database
load seed data
dump the whole db as text
do some stuff
dump again
then diff the two!
Using mongodump and bsondump:
Step 1
Dump the entire database to BSON files:
mongodump --db db1
Step 2
Convert each BSON file to JSON file:
for f in dump/db1/*.bson; do bsondump "$f" > "$f.json"; done
Hope it's helps!
You can use mongoexport utility. It will dump out json by default but you can specify to dump out csv format.
mongoexport --help will give you all the options you will need.
You mentioned that you would like to dump all the collections from a database, and mongoexport expects you to specify a database and collection name, since it expects to dump one collection.
You can write a short shell script (if you are on Windows, convert to batch script) to the effect of:
#!/bin/sh
# assuming mongo bin is in your path
host=YOURMONGOHOST
port=YOURMONGOPORT
db=DBYOUWANTTOEXPORT
for c in `mongo --quiet $host:$port/$db --eval 'db.getCollectionNames()' | sed 's/,/ /g'`
do
mongoexport --host $host --port $port -d $db -c $c > $c.json
done
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.