The MongoDB shell is more than just an interactive shell, it also supports execution of specified JavaScript files, and also supports execution of specified command fragments. With this feature, MongoDB can be combined with the Linux shell to do most of the daily management and maintenance work.
executes the specified command
For example, there are several common ways to query the number of records in the T1 table of the test library, as follows:
[Email protected] bin]#/1.8.1connecting to:test> db.t1.count ()7>
Execute the statement directly from the command line eval parameter:
[Email protected] bin]#/mongo test--eval "Printjson (Db.t1.count ())"1.8.1connecting to:test7
execute the specified file
If it involves a lot of operations to get the results, then it is impossible to do so with eval, so the more flexible way to execute the specified file comes in handy. For example, we still want to see the number of records in the test Library T1 table:
T1_count.js is the file we are going to execute, the contents are as follows
= Db.t1.count ();p Rintjson (' total count of T1 is: ' + totalcount ';p rintjson ('-----------------------') );
Below we will execute this file
[Email protected] bin]#./1.8.1connecting to:test"Total count of T1 is:7" "-----------------------"
You can see that finally get the T1 table record number 7, then some unnecessary descriptive text we do not want to appear, how to deal with it?
[[email protected] bin]#./mongo--quiet t1_count.js"total count of T1 is:7" "-----------------------"[[EMA Il protected] bin]#
By specifying the quiet parameter, you can mask some login information, which can make the results clearer.
MongoDB finishing notes specify commands and specified files