I am trying to use scripts js in the mongo shell. I am reading a book on the topic. I have tried several options, such as opening the cmd at a specific place at my computer.
I found here the question "How to execute mongo commands through shell scripts?", but nothing seems to work for me. My mongo is working properly and I can enter mongo simply typing mongo at cmd. However, it cannot find the js files.
I am using Windows; anything has an idea what may be happening under the hood?
An example after:
mongo demo.js
2020-02-20T11:03:33.098-0300 E - [main] file [demo.js] doesn't exist
2020-02-20T11:03:33.098-0300 F - [main] failed to load: demo.js
2020-02-20T11:03:33.098-0300 E - [main] exiting with code -3
Create your my_script.js file with this one command:
db.testColl.insertOne( { a: "hello" } )
Place the script file in your current directory.
1. Run JS Script from OS Command-line:
From OS prompt do this:
> mongo localhost/testDB my_script.js
Once the above command is run, you will see the output as follows (similar, depending upon your MongoDB version and the OS (Windows, in this case)):
MongoDB shell version v4.2.3
connecting to: mongodb://localhost:27017/testdb?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("456b350f-668f-4389-9901-7c456e2c50fe") }
MongoDB server version: 4.2.3
Now, from the Mongo Shell (i.e., do mongo, and then from the mongo prompt):
mongo > use testDB
mongo > show collections
You will see the testColl listed.
mongo > db.testColl.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
This is the document created in the testDB database and the collection testColl as per the command in the my_script.js.
2. Run JS Script from Mongo Shell:
Also, you can run the my_script.jsfrom within the Mongo Shell.
mongo > load("my_script.js")
true
mongo > db.test.find()
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
See there are two documents with different _id field values.
NOTE: I think you can run only some commands from the .js file.
3. Another Example:
Create a JS file named script2.js with the following content:
db.test.find().forEach(printjson)
Note the printjson shell method prints a document to the shell output.
mongo > load("script2.js")
{ "_id" : ObjectId("5e4ea0d05816162b300b0346"), "a" : "hello" }
{ "_id" : ObjectId("5e4ea10f276cde8fc5fedec5"), "a" : "hello" }
References:
load()
Print document value in
MongoShell
New to MongoDB - at the moment I'm creating indexes directly from my web app however I want to instead have some sort of bash script I run (and more easily maintain) that can create my various text indexes for me.
Wanted to check is this possible? I'm unsure about how I would actually execute it if so - namely I have a Docker image running Docker - so do I have to bash into that then run the .sh? Or would I just specify the DB and collection in the script itself and just run it from terminal as usual?
Any pointers would be appreciated.
Thanks.
You can do it using java script:
var createIndexes = function(fullObj) {
conn = new Mongo();
db = conn.getDB(databaseName);
getMapInd = null;
setMapInd1 = db.testMappings.createIndex( { 'testId': 1}, {unique: true} )
getMapInd = db.testMappings.getIndexes();
printjson("---------------------Below indexes created in Mappings collection-----------------------");
printjson(getMapInd);
};
createIndexes();
I have a javascript file and want to run them inside a mongo shell. I know how to load the javascript file during connection from this link: How to execute mongo commands through shell scripts?.
But my case is how to run an external js file after the connection already established.
My js file looks like below:
db.getSiblingDB("test")
.enron_messages.find({
$or: [{ filename: "111." }, { "headers.From": "test#gmail.com" }]
})
.sort({ "headers.To": 1 })
.limit(10);
and in my mongo shell, I use load('test.js') to load that file but it returns true(see below output). How to execute this file?
> load('test.js')
true
The load() function does in fact execute the javascript file. The problem you are facing is that when the external scripts are executed in "scripted" mode, find() will only return the cursor and not iterate it as the mongo shell does when run in Interactive mode.
This behavior is detailed in the docs in Write Scripts for the Mongo shell. There you will see an example of how you need to iterate the cursor and print the results:
cursor = db.collection.find();
while ( cursor.hasNext() ) {
printjson( cursor.next() );
}
I have a collection I want to drop / rename. I have written the following javascript to do this:
var now = new Date();
var runTime = now.getTime();
var newCollName = "myColl." + runTime;
db.myColl.renameCollection(newCollName);
When I run the script on my single node dev install the drop or rename works fine. However when I try the same command on the real instance which is clustered and uses replicasets I get the following error:
Renaming myColl collection to : myColl.1449761151171
2015-12-10T15:25:55.703+0000 E QUERY Error: write $cmd not supported in SyncClusterConnection::query for:renameCollection
at DBQuery._exec (src/mongo/shell/query.js:83:36)
at DBQuery.hasNext (src/mongo/shell/query.js:240:10)
at DBCollection.findOne (src/mongo/shell/collection.js:187:19)
at DB.runCommand (src/mongo/shell/db.js:58:41)
at DB.adminCommand (src/mongo/shell/db.js:66:41)
at DBCollection.renameCollection (src/mongo/shell/collection.js:642:21)
I've read the mongodb notes for drop and renameCollection, but they dont mention needing to do anything special when running the commands on clustered database.
What do I need to do to archive off a collection on a MongoDB that is structured like this?
thanks.
I am using Robo 3T (formerly RoboMongo) which I connect to a MongoDB. What I need to do is this: There is a collection in that MongoDB. I want to export the data from that collection so that I can save it into a file.
I used the interface to open the data from the collection as text and did a Ctrl + A and pasted into a text file. However, I found that not all data is copied and also that there were many comments in the text data which naturally breaks the JSON.
I am wondering if Robo 3T has a "Export As JSON" facility so that I can do a clean export.
Any pointers are appreciated!
A quick and dirty way: Just write your query as db.getCollection('collection').find({}).toArray() and right click Copy JSON. Paste the data in the editor of your choice.
You can use tojson to convert each record to JSON in a MongoDB shell script.
Run this script in RoboMongo:
var cursor = db.getCollection('foo').find({}, {});
while(cursor.hasNext()) {
print(tojson(cursor.next()))
}
This prints all results as a JSON-like array.
The result is not really JSON! Some types, such as dates and object IDs, are printed as JavaScript function calls, e.g., ISODate("2016-03-03T12:15:49.996Z").
Might not be very efficient for large result sets, but you can limit the query. Alternatively, you can use mongoexport.
Robomongo's shell functionality will solve the problem. In my case I needed couple of columns as CSV format.
var cursor = db.getCollection('Member_details').find({Category: 'CUST'},{CustomerId :1,Name :1,_id:0})
while (cursor.hasNext()) {
var record = cursor.next();
print(record.CustomerID + "," + record.Name)
}
Output : -------
334, Harison
433, Rechard
453, Michel
533, Pal
you say "export to file" as in a spreadsheet? like to a .csv?
IMO this is the EASIEST way to do this in Robo 3T (formerly robomongo):
In the top right of the Robo 3T GUI there is a "View Results in text
mode" button, click it and copy everything
paste everything into this website: https://json-csv.com/
click the download button and now you have it in a spreadsheet.
hope this helps someone, as I wish Robo 3T had export capabilities
There are a few MongoDB GUIs out there, some of them have built-in support for data exporting. You'll find a comprehensive list of MongoDB GUIs at http://mongodb-tools.com
You've asked about exporting the results of your query, and not about exporting entire collections. Give 3T MongoChef MongoDB GUI a try, this tool has support for your specific use case.
Don't run this command on shell, enter this script at a command prompt with your database name, collection name, and file name, all replacing the placeholders..
mongoexport --db (Database name) --collection (Collection Name) --out (File name).json
It works for me.
I don't think robomongo have such a feature.
So you better use mongodb function as mongoexport for a specific Collection.
http://docs.mongodb.org/manual/reference/program/mongoexport/#export-in-json-format
But if you are looking for a backup solution is better to use
mongodump / mongorestore
If you want to use mongoimport, you'll want to export this way:
db.getCollection('tables')
.find({_id: 'q3hrnnoKu2mnCL7kE'})
.forEach(function(x){printjsononeline(x)});
Expanding on Anish's answer, I wanted something I can apply to any query to automatically output all fields vs. having to define them within the print statement. It can probably be simplified but this was something quick & dirty that works great:
var cursor = db.getCollection('foo').find({}, {bar: 1, baz: 1, created_at: 1, updated_at: 1}).sort({created_at: -1, updated_at: -1});
while (cursor.hasNext()) {
var record = cursor.next();
var output = "";
for (var i in record) {
output += record[i] + ",";
};
output = output.substring(0, output.length - 1);
print(output);
}
Using a robomongo shell script:
//on the same db
var cursor = db.collectionname.find();
while (cursor.hasNext()) {
var record = cursor.next();
db.new_collectionname.save(record);
}
Using mongodb's export and import command
You can add the --jsonArray parameter / flag to your mongoexport command, this exports the result as single json array.
Then just specify the --jsonArray flag again when importing.
Or remove the starting and ending array brackets [] in the file, then your modified & exported file will import with the mongoimport command without the --jsonArray flag.
More on Export here: https://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray
Import here:
https://docs.mongodb.org/manual/reference/program/mongoimport/#cmdoption--jsonArray
Solution:
mongoexport --db test --collection traffic --out traffic.json<br><br>
Where:
database -> mock-server
collection name -> api_defs
output file name -> childChoreRequest.json
An extension to Florian Winter answer for people looking to generate ready to execute query.
drop and insertMany query using cursor:
{
// collection name
var collection_name = 'foo';
// query
var cursor = db.getCollection(collection_name).find({});
// drop collection and insert script
print('db.' + collection_name + '.drop();');
print('db.' + collection_name + '.insertMany([');
// print documents
while(cursor.hasNext()) {
print(tojson(cursor.next()));
if (cursor.hasNext()) // add trailing "," if not last item
print(',');
}
// end script
print(']);');
}
Its output will be like:
db.foo.drop();
db.foo.insertMany([
{
"_id" : ObjectId("abc"),
"name" : "foo"
}
,
{
"_id" : ObjectId("xyz"),
"name" : "bar"
}
]);
I had this same issue, and running script in robomongo (Robo 3T 1.1.1) also doesn't allow to copy values and there was no export option either.
The best way I could achieve this is to use mongoexport, if mongodb is installed on your local, you can use mongoexport to connect to database on any server and extract data
To connect to Data on remote server, and csv output file, run the following mongoexport in your command line
mongoexport --host HOSTNAME --port PORT --username USERNAME --password "PASSWORD" --collection COLLECTION_NAME --db DATABASE_NAME --out OUTPUTFILE.csv --type=csv --fieldFile fields.txt
fieldFile: helps to extract the desired columns, ex:
contents of fields.txt can be just:
userId
to only extract values of the column 'userId'
Data on remote server, json output file:
mongoexport --host HOST_NAME --port PORT --username USERNAME --password "PASSWORD" --collection COLECTION_NAME --db DATABASE_NAME --out OUTPUT.json
this extracts all fields into the json file
data on localhost (mongodb should be running on localhost)
mongoexport --db DATABASE_NAME --collection COLLECTION --out OUTPUT.json
Reference: https://docs.mongodb.com/manual/reference/program/mongoexport/#use
Simple solution:
tostrictjson(db.getCollection(collection_name).find({}))
Note:
Other solutions are fine but might cause errors during import when your collection has types like Date, ObjectId etc...
Happy Hacking :)
I export using Mongodb Compass, you can export to csv or json.
On the menu of Mongo Compass select Collection-> export collection, and you can select the fields to export, and the file to export the result, previously you can specify the query.
Regards
make your search
push button view results in JSON mode
copy te result to word
print the result from word