MongoDB Command Line Query db.Collection.find().pretty() Not Working - mongodb

I've read the documentation about query to load data in mongodb database. What I've done :
Open Command Line (Windows)
Start Mongod and Mongo
use first_test
db.table1.insert({"Column 1":"Data 1"})
Then I try db.table1.find().pretty() But The result not return as it must be like in documentation. Is there anything wrong in my command line or something else ?

your document has to have at least 3 fields to be prettyfied

Related

mongoimport rejects NumberDecimal types

Mongodb from v.3.4 supports NumberDecimal
I have MongoDB server version: 3.4.1, but when I execute mongoimport shell command I get an error:
Failed: error processing document #7: invalid character 'D' in literal NumberInt or NumberLong (expecting 'I' or 'L')
My json:
{"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":NumberDecimal("95")}
Normal insert executes ok, so this is mongoimort problem.
db.aaa.insert({"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":NumberDecimal("95")})
WriteResult({ "nInserted" : 1 })
Why is that? Can you help me?
One trick to figure out the correct extended json format is to use the mongoexport utility on a collection to see how MongoDB itself publishes the json out.
In this case the following should work:
{"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":{"$numberDecimal":"95"}}
I tested this with mongoimport with server version 3.4.1 and it imports the numberDecimal correctly.
NumberDecimal("95") is not a valid JSON representation. It should be
{"c":"7E474601-B511-4AD9-B2B8-7E61807F9673","n":"n1","v":95}
It works with mongo shell because the shell is a javascript interpreter that treats NumberDecimal as a valid data type.
If you need NumberDecimal data type you need to use a driver or the mongo shell.
Form a bash shell you can run this simple script:
for i in `cat aaa.json`
do
mongo --quiet --eval "db.aaa.insert($i)"
done
Please note that this is row by row insertion and not very efficient.

MongoDb - Export database to js script (similar to rockmongo export)

Is there a way from the command line that I can dump a MongoDb database to a javascript file that can be interpreted by the mongo shell? I am looking for a way to do exactly what the RockMongo Export function does, but I need to be able to call it from a command line script. I've looked everywhere for something that does this but all I can seem to find is mongoexport and mongodump which don't seem to do what I want, as these just create JSON files.
The reason I need to do this is because codeception's MongoDb module requires a file in this format to restore the database after each test. I want to write a script to automate this process so that I don't have to constantly go through RockMongo and generate the dump.
Thanks in advance!
In case anyone else happens to find this, I finally found a solution that works for my scenario. I had to take Markus' suggestion and kind of roll my own solution, but I discovered a mongodb command called bsondump that made things much easier.
So in my script I first use mongodump to create a BSON file of my collection
mongodump --db mydb --collection mycollection --out - > mycollection.bson
I then use bsondump to convert that into JSON that can be used in Shell Mode
bsondump mycollection.bson > mycollection.json
Finally, I'm using PHP so in my PHP script I loop through that json file and wrap each line in an insert statement.
$lines = file('mycollection.json');
$inserts = [];
foreach($lines as $line)
{
$inserts[] = 'db.getCollection("mycollection").insert(' . trim($line) . ');' . PHP_EOL;
}
file_put_contents('output.js', $inserts);
I'm guessing there is probably a better way to do this, but so far this seems to be working nicely for me. Thanks for steering me in the right direction Markus!

Printing Mongo query output to a file while in the mongo shell

2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with Mongo, while being in the shell
I can easily get the output of a query I want by being outside of the shell and executing the following command:
mongo localhost:27017/dbname --eval "printjson(db.collectionName.findOne())" > sample.json
The above way is fine, but it requires me to exit the mongo shell or open a new terminal tab to execute this command. It would be very convenient if I could simply do this while still being inside the shell.
P.S: the Question is an offshoot of a question I posted on SO
AFAIK, there is no a interactive option for output to file, there is a previous SO question related with this: Printing mongodb shell output to File
However, you can log all the shell session if you invoked the shell with tee command:
$ mongo | tee file.txt
MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye
Then you'll get a file with this content:
MongoDB shell version: 2.4.2
connecting to: test
> printjson({this: 'is a test'})
{ "this" : "is a test" }
> printjson({this: 'is another test'})
{ "this" : "is another test" }
> exit
bye
To remove all the commands and keep only the json output, you can use a command similar to:
tail -n +3 file.txt | egrep -v "^>|^bye" > output.json
Then you'll get:
{ "this" : "is a test" }
{ "this" : "is another test" }
We can do it this way -
mongo db_name --quiet --eval 'DBQuery.shellBatchSize = 2000; db.users.find({}).limit(2000).toArray()' > users.json
The shellBatchSize argument is used to determine how many rows is the mongo client allowed to print. Its default value is 20.
If you invoke the shell with script-file, db address, and --quiet arguments, you can redirect the output (made with print() for example) to a file:
mongo localhost/mydatabase --quiet myScriptFile.js > output
There are ways to do this without having to quit the CLI and pipe mongo output to a non-tty.
To save the output from a query with result x we can do the following to directly store the json output to /tmp/x.json:
> EDITOR="cat > /tmp/x.json"
> x = db.MyCollection.find(...).toArray()
> edit x
>
Note that the output isn't strictly Json but rather the dialect that Mongo uses.
In the new mongodb shell 5.0+ mongosh, it integrate the Node.js fs module, so you can simply do below in the new mongosh shell:
fs.writeFileSync('output.json', JSON.stringify(db.collectionName.findOne()))
This also avoid problems such as the ObjectId(...) being included in the tojson result, which is not valid JSON string.
The above code works according to the docs describes:
The MongoDB Shell, mongosh, is a fully functional JavaScript and Node.js 14.x REPL environment for interacting with MongoDB deployments. You can use the MongoDB Shell to test queries and operations directly with your database.
The old mongo shell already marked as Legacy, so use the mongosh if possible.
It may be useful to you to simply increase the number of results that get displayed
In the mongo shell > DBQuery.shellBatchSize = 3000
and then you can select all the results out of the terminal in one go and paste into a text file.
It is what I am going to do :)
(from : https://stackoverflow.com/a/3705615/1290746)
Combining several conditions:
write mongo query in JS file and send it from terminal
switch/define a database programmatically
output all found records
cut initial output lines
save the output into JSON file
myScriptFile.js
// Switch current database to "mydatabase"
db = db.getSiblingDB('mydatabase');
// The mark for cutting initial output off
print("CUT_TO_HERE");
// Main output
// "toArray()" method allows to get all records
printjson( db.getCollection('jobs').find().toArray() );
Sending the query from terminal
-z key of sed allows treat output as a single multi-line string
$> mongo localhost --quiet myScriptFile.js | sed -z 's/^.*CUT_TO_HERE\n//' > output.json

Can't access collection from the shell - SyntaxError: missing ; before statement (shell):1

I wrote a script that uses mongoimport to load csv files into mongodb. When I run this for two similar csv files (of the same type) both upload fine, however I can only access one of them from the mongodb shell. Here is a transcript of a mongodb shell session:
> show collections
3mLgQAYJCq6_20110802
eTByWMY7zO6_20110802NonUniCode
system.indexes
> db.3mLgQAYJCq6_20110802
Thu Aug 18 18:44:49 SyntaxError: missing ; before statement (shell):1
> db.eTByWMY7zO6_20110802NonUniCode
vh.eTByWMY7zO6_20110802NonUniCode
However, I can access both collections from a python script and using mongoexport. I suspect there is a problem with the 3mLgQAYJCq6_20110802 file but I don't know where to start looking. Any ideas?
This works for me when my collection names include special characters:
db["3mLgQAYJCq6_20110802"].findOne();
The collection name should start with a letter or the underscore.You can get the info about the naming convention for the collections in mongodb from below link-
http://www.mongodb.org/display/DOCS/Collections
lovely collection names... it's probably because it's starts with a 3.

MongoDB : Show databases like MySQL

My MongoDB has more than 100 databases in it.
Whenever i use show dbs command my screen is filled with all databases names and it makes hard to find a particular database.
How to display only those databases which contain a particular substring as we can query in MySQL for displaying particular databases with ( show databases like '%SUBSTR%' ) query.
We do not have options like that. But your problem can be resolved by outputting the result into a txt file and later opening it
$ mongo | tee outnew.txt
In the mongo shell you can the give the show dbs command and exit.
mongo> show dbs;
mongo> exit
Then using gedit or excel access the outnew.txt file.
Hope it helped.
Another option is:
> db.getMongo().getDBNames().forEach(
function(databaseName) {
if (databaseName.match(/SUBSTR/i))
print(databaseName);
}
);
> var showdbs = function(pattern) {
db.getMongo().getDBNames().forEach(
function(databaseName) {
if (databaseName.match(new RegExp(pattern, 'i')))
print(databaseName);
});
};
> showdbs('SUBSTR'); // ALL: showdbs();
If you are on a *nix OS you could run the following command.
mongo --eval "db.adminCommand('listDatabases')['databases']" | grep "SUBSTR"
Note: You need to be admin to run this command.