3

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?

5
  • What does explain() method return? Commented Apr 27, 2015 at 14:03
  • 1
    Yes it is possible, here is the manual for this topic: docs.mongodb.org/manual/tutorial/… Commented Apr 27, 2015 at 14:04
  • the explain() method returns some useful information about the exexution of the query here is a example: docs.mongodb.org/manual/reference/explain-results @IvanJ Commented Apr 27, 2015 at 14:19
  • I have looked at the manual but havent found it useful I was hoping for get a example or easier to understand explaination @user3415653 Commented Apr 27, 2015 at 14:21
  • Not sure how you can save file with JavaScript. If you are trying to write a file on client machine, You can't do this in any cross-browser way. Commented Apr 27, 2015 at 14:44

2 Answers 2

2

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

var temp = db.tweets.find({},{"user.name":1}).explain();
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.