0

I have written a very simple script to print the count of some states of a worker script. However I cannot print error messages which I had queried and projected with find(). Here is the script:

conn = new Mongo();
db = conn.getDB("AABackend");
...
print("Error messages:");
print(db.jobs.find({"state":"failed"}, {error_message:1}));

It's the last line that doesn't work. I expect something like:

{ "_id" : ObjectId("55377e5293671115faf44b15"), "error_message" : "foobar" }

but I get

DBQuery: AABackend.jobs -> { "state" : "failed" }

I couldn't find something in the MongoDB documentation about this but just a hint would really help.

dieser_K3
  • 250
  • 3
  • 13
  • You probably need to use **`findOne()`** method rather than **`find()`** as the latter only returns a cursor not the actual documents. – chridam Apr 22 '15 at 12:17
  • You are currently printing the contents of a cursor rather than the results of the query (see: [Printing mongodb shell output to File](http://stackoverflow.com/questions/13104800/printing-mongodb-shell-output-to-file)). – Stennie Apr 23 '15 at 08:35
  • Thanks for the link, it's indeed a duplicate. I didn't realized that this answer fits to my usecase as I'm totally new to this database and especially MongoDB stuff :) – dieser_K3 Apr 23 '15 at 09:18

1 Answers1

0

I'm not sure which driver this is, but in Node.js, that would return the cursor object and I think that's what you are getting printed out. You are printing the cursor object and not the results. It's probably why it prints DBQuery: .......

In Node.js I would do something like this:

db.jobs.find({"state":"failed"}).toArray(function(err, docs) {
    console.log(docs);
};

Basically try to find how to get the results from a cursor object in your language's driver --> equivalent to toArray()

Hope this helps