Running JavaScript file to fetch mongodb requests

I have a list of .find () requests, for example:

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

      

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

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

Is it possible? and how can i achieve this?

+3


source to share


2 answers


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"
}

      

+1


source


Since the method explain()

returns JSON, you can store it in a variable.



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

      

+2


source







All Articles