Skip to content

Instantly share code, notes, and snippets.

@mehdishahdoost
Last active July 2, 2023 19:56
Show Gist options
  • Select an option

  • Save mehdishahdoost/6b6fc8bb2720fcae1655267e9773d6cc to your computer and use it in GitHub Desktop.

Select an option

Save mehdishahdoost/6b6fc8bb2720fcae1655267e9773d6cc to your computer and use it in GitHub Desktop.
MongoDB Reference

Best file system for Mongodb

XFS is a great choice as a filesystem, as it can perform snapshot backups.

Document Max (best practice) Size

Respect the maximum document size of 16 MB

MongoDB JIRA Address

JIRA Link

Data types

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

MongoDB command line (mongosh)

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

MongoDB Scripts Samples

  var title = 'Test5';
  db.books.insertOne({title: title})   
queryTitle = function(title) {printjson(db.books.find({title: title}))}
queryTitle("test3")

Script in file

  • 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()

Script file sample

db = db.getSiblingDB('dilmachi');
print("Hello");
queryTitle = function(title) {printjson(db.books.find({title: title}))}
queryTitle("test3")

Insert many documents via script

db = db.getSiblingDB('dilmachi');
insertBulk = function() {
  
  for(loop=0;loop<1000;loop++) {
     db.books.insertOne({name: "Moon"});			
  }
}

insertBulk();

Insert many documents via script with Bulk commands

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();

bulk update

db = db.getSiblingDB('dilmachi');
insertBulk = function() {
	
	var bulk = db.books.initializeOrderedBulkOp();
	bulk.find({name: 'Moon'}).update({$set: {price: 300}});
	bulk.execute();
}

insertBulk();				     

Get running commands in the server

use admin
db.runCommnand({"currentOp": 1});

Create an User

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment