Mongo shell commands can be executed in console server side script mode. The inputs are directly forwarded to the db.command() function of the node.js MongoDB driver and are not formatted or modified by the application.
Codeblock |
---|
|
// Returns all documents
db.zipcode.find();
// Returns all cities name and sorted by them
db.zipcode.find({}, {city: 1}).sort({city: 1})
// Create a new zipcode document
db.zipcode.insert({_id: '99999', city: 'LEIPZIG', pop: 520838, state: 'SN'})
// Analyze the find-query
db.zipcode.find().explain()
// Show the statistics of the collection
db.zipcode.stats(); |
Warnung |
---|
There is no pager in console server side script mode. Therefore, all records are loaded for each query. Because this can be a very large amount of data in certain circumstances, the results are limited to 100 documents. It is recommended to use the skip () and limit () functions. |
Info |
---|
When using a single a single command, the semicolon at the end is optional . |
You can enter multiple commands that have to be included in a function and the result must be returned with the return statement.
Codeblock |
---|
|
function () {
var stats = db.zipcode.stats();
var index = db.zipcode.getIndexes();
return {
stats: stats,
indexInformation: index
};
} |
Hinweis |
---|
The console server side script mode only works if the server instance of Mongo runs with enabled server-side JavaScript. It is enabled by default. |
Hinweis |
---|
Since MongoDB > 2.6 the server side script mode does not work when authentication is enabled because then the server-side JavaScript is disabled in MongoDB. |