XFS is a great choice as a filesystem, as it can perform snapshot backups.
Respect the maximum document size of 16 MB
MongoDB uses BSON, a binary-encoded serialization for JSON documents. BSON extends the JSON data types, offering, for example, native data and binary data types
| Command | Description |
|---|---|
| mongosh.exe -u root -p root | login into localhost database |
| show dbs | list all databases |
| db | print current database name |
| use | switch to another database |
| db.books.insertOne({title: 'test'}) | create a new collection books in the db and insert a document into it |
| db.books.find() | retrieves all documents in the collection |
| db.books.deleteOne({title: 'test'}) | delete one document in the collection |
| db.books.deleteMany({title: 'test'}) | delete many documents in the collection |
| db.books.updateMany({title: 'test2'}, {$set: {title: 'test3'}}) | Update documents |
| db.books.updateOne({title: 'test2'}, {$set: {title: 'test3'}}) | Update document |
| db.books.updateOne({title: 'test2'}, {$set: {price: 20}}) | Add new field to document |
| show collections | return all collections |
var title = 'Test5';
db.books.insertOne({title: title}) queryTitle = function(title) {printjson(db.books.find({title: title}))}
queryTitle("test3")- Run script
mongosh .js
When writing scripts for the mongo shell, we cannot use shell helpers. MongoDB’s commands—such as use <database_name>, show collections, and other helpers—are built into the shell and so are not available from the JavaScript context where our scripts will get executed. Fortunately, there are equivalents to them that are available from the JavaScript execution context
| Helpers | JS Version |
|---|---|
| show dbs | db.adminCommand('listDatabases') |
| use | db = db.getSibilingDB('db-name') |
| show collections | db = db.getCollectionNames() |
db = db.getSiblingDB('dilmachi');
print("Hello");
queryTitle = function(title) {printjson(db.books.find({title: title}))}
queryTitle("test3")db = db.getSiblingDB('dilmachi');
insertBulk = function() {
for(loop=0;loop<1000;loop++) {
db.books.insertOne({name: "Moon"});
}
}
insertBulk();db = db.getSiblingDB('dilmachi');
insertBulk = function() {
var bulk = db.books.initializeUnorderedBulkOp();
for(loop=0;loop<1000;loop++) {
bulk.insert({name: "Moon"});
}
bulk.execute();
}
insertBulk(); we used initializeUnorderedBulkOp() for the bulk operation builder setup. The reason we did this is that we don’t care about the order of insertions being the same as the order in which we add them to our bulk variable with the bulk.insert() command.
db = db.getSiblingDB('dilmachi');
insertBulk = function() {
var bulk = db.books.initializeOrderedBulkOp();
for(loop=0;loop<1000;loop++) {
bulk.insert({name: "Moon"});
}
bulk.execute();
}
insertBulk();db = db.getSiblingDB('dilmachi');
insertBulk = function() {
var bulk = db.books.initializeOrderedBulkOp();
bulk.find({name: 'Moon'}).update({$set: {price: 300}});
bulk.execute();
}
insertBulk(); use admin
db.runCommnand({"currentOp": 1});
db.createUser(
{
user: <adminUser>,
pwd: <password>,
roles: [ { role: "dbAdmin", db: "mongo_book" } ]
}
)References
- Mastering MongoDB 6.x: Expert techniques to run high-volume and fault-tolerant database solutions using MongoDB 6.x, 3rd Edition