Pretty print in MongoDB shell as default - mongodb

Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.

(note: this is answer to original version of the question, which did not have requirements for "default")
You can ask it to be pretty.
db.collection.find().pretty()

You can add
DBQuery.prototype._prettyShell = true
to your file in $HOME/.mongorc.js to enable pretty print globally by default.

(note: this is answer to the updated question)
You can just do this on the CLI:
echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js
And it's always going to output pretty results.

Since it is basically a javascript shell, you can also use toArray():
db.collection.find().toArray()
However, this will print all the documents of the collection unlike pretty() that will allow you to iterate.
Refer: http://docs.mongodb.org/manual/reference/method/cursor.toArray/

Oh so i guess .pretty() is equal to:
db.collection.find().forEach(printjson);

Give a try to Mongo-hacker(node module), it alway prints pretty.
https://github.com/TylerBrock/mongo-hacker
More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like
Colorization
Additional shell commands (count documents/count docs/etc)
API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
Aggregation Framework
I am using for while in production env, no problems yet.

Got to the question but could not figure out how to print it from externally-loaded mongo. So:
This works is for console: and is prefered in console, but does not work in external mongo-loaded javascript:
db.quizes.find().pretty()
This works in external mongo-loaded javscript:
db.quizes.find().forEach(printjson)

Check this out:
db.collection.find().pretty()

Related

TYPO3 queryBuilder: How to work with BINARY in where() clause?

I just have a short question.
There's no description in the following API overview of TYPO3 how to use a "BINARY" in where() clause: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/Database/QueryBuilder/Index.html#expr
What I want to achieve? this one:
WEHRE BINARY `buyer_code` = "f#F67d";
Actually I can only do the following:
->where(
$queryBuilder->expr()->eq('buyer_code', 'f#F67d')
);
But in this case I don't get a satisfying result for myself because I need case-sensitive here :-)
An another buyer_code exists "f#F67D" (the last char is uppercase) but I do need to look for the other one.
Thanks for helping.
Since TYPO3 is using Doctrine API here, you could try to do
->where('BINARY `buyer_code` = ' . $queryBuilder->createNamedParameter('f#F67d'))
Please keep in mind, that this query now only works for database backends, supporting the BINARY keyword!
Please have a look at Doctrine2 case-sensitive query The thread is a bit older, but seems to cover background and solution for your problem.

How to make the result displayed as multiply line NOT one line for mongodb [duplicate]

Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.
(note: this is answer to original version of the question, which did not have requirements for "default")
You can ask it to be pretty.
db.collection.find().pretty()
You can add
DBQuery.prototype._prettyShell = true
to your file in $HOME/.mongorc.js to enable pretty print globally by default.
(note: this is answer to the updated question)
You can just do this on the CLI:
echo DBQuery.prototype._prettyShell = true >> ~/.mongorc.js
And it's always going to output pretty results.
Since it is basically a javascript shell, you can also use toArray():
db.collection.find().toArray()
However, this will print all the documents of the collection unlike pretty() that will allow you to iterate.
Refer: http://docs.mongodb.org/manual/reference/method/cursor.toArray/
Oh so i guess .pretty() is equal to:
db.collection.find().forEach(printjson);
Give a try to Mongo-hacker(node module), it alway prints pretty.
https://github.com/TylerBrock/mongo-hacker
More it enhances mongo shell (supports only ver>2.4, current ver is 3.0), like
Colorization
Additional shell commands (count documents/count docs/etc)
API Additions (db.collection.find({ ... }).last(), db.collection.find({ ... }).reverse(), etc)
Aggregation Framework
I am using for while in production env, no problems yet.
Got to the question but could not figure out how to print it from externally-loaded mongo. So:
This works is for console: and is prefered in console, but does not work in external mongo-loaded javascript:
db.quizes.find().pretty()
This works in external mongo-loaded javscript:
db.quizes.find().forEach(printjson)
Check this out:
db.collection.find().pretty()

Update in MongoDB Meteor

Is there any scenario that explains why the update is not working, I can't seem to pinpoint the cause and I don't see any errors whatsoever. Is there a way to check the output of the update function because currently the update is not doing anything. That means the last log line shows a value different than 20170615-7702.
Db.find().forEach(function(item){
console.log(item._id+ " =======> " + item.build.parameters.BUILD_NUM);
Db.update({"_id":item._id}, {$set:{"build.parameters.BUILD_NUM":"20170615-7702"}});
console.log(Db.findOne({"_id": item._id}).build.parameters.BUILD_NUM);});
Thank you
The code above should work. I think that the problem above is that it is not able to update the document as fast as it prints the output. First try:
var itemUpdated = Db.findOne({"_id": item._id});
console.log(itemUpdated._id)
The other option is very simple go to the command line and run meteor mongo. Then see all the entries in the collection and their properties.
Third and maybe the best option to test here is to use setTimeout() for the console.log().
Hope it helps.

How to use distinct in MongoVue?

I need to find out distinct values(for example : CreationDate or SourceSystem) in MongoDB using MongoVUE. FYI, I can only use the trial version of the same.
I don't think you can do that using MongoVUE. You can do it through MongoDB shell running a command like this:
db.[Collection Name].distinct({Property Name})
ex: db.students.distinct('age')
db.students.distinct('age').length; // gives you the record count
I usually find SQL to Mongo Mapping Chart page useful in these case ( http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart )
From having a quick look, I can't see how you can either, as the "Find" feature forces you to use:
db.[collectionName].find({...})
so doesn't allow you to do:
db.[collectionName].distinct({...})
I recommend using the normal command line executable for Mongo instead of MongoVUE, then you can use the commands from the 10Gen documentation:
http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

Zend Lucene - cannot search numbers

Using Zend Lucene I cannot search numbers in description fields
Added it like this:
$doc->addField(Zend_Search_Lucene_Field::Text('description', $current_item['item_short_description'], 'utf-8'));
Googling for this showed that applying following code should solve the problem, but it did not..:
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
any thougts?
You have to set the default analyzer twice: On the indexing process as well as on the searching process.
Use the code line from above:
Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive());
Did you use that command before or after calling Zend_Search_Lucene::open()?
Calling it beforehand definitely works.
I'm not sure about 'zend', but for deal with number in lucene, you need use following technique:
To place int to document use following:
document.Add(new Field(FIELD_SPEC, NumberTools.LongToString(YOUR_INT), Field.Store.YES, Field.Index.UN_TOKENIZED));
To locate value use Term: Term(FIELD_SPEC, NumberTools.LongToString(YOUR_INT))