mike
mike

Reputation: 33

running JavaScript file to excute mongodb queries

I have for example a list of .find() queries like this:

db.tweets.find({},{"user.name":1}).explain()

I want to run the queries from a javascript file then save outputs to a text file

the important thing for me is getting the results from the .explain() in the text file too

is this possible? and how can I achieve this?

Upvotes: 3

Views: 86

Answers (2)

IvanJ
IvanJ

Reputation: 645

Since explain() method returns JSON, you can save it into a variable.

var temp = db.tweets.find({},{"user.name":1}).explain();

Upvotes: 2

user3415653
user3415653

Reputation: 335

example script test.js:

conn = new Mongo('hostname');
db = conn.getDB('dbName');

var temp =  db.collection.find().explain()

// do what ever you want
printjson(temp)

run on command line:

mongo hostname test.js

output:

MongoDB shell version: 2.4.9 connecting to: hostname/test 
{
  "cursor": "BasicCursor",
  "isMultiKey": false,
  "n": 4795,
  "nscannedObjects": 4795,
  "nscanned": 4795,
  "nscannedObjectsAllPlans": 4795,
  "nscannedAllPlans": 4795,
  "scanAndOrder": false,
  "indexOnly": false,
  "nYields": 0,
  "nChunkSkips": 0,
  "millis": 2,
  "indexBounds": {
  },
  "server": "XXX:XXX"
}

Upvotes: 1

Related Questions