How to print a sentence when users on the server run mongo shell?
For
By default
That's it! Save the file and run
In order to disable dangerous functionalities:
Sample output:
#mongodb #mongo #shell #mongorc
For
DBAs
to limit some dangerous functionalities like dropping a database or it can be a helpful message. Or a greeting message. Or even printing the default database that he is already connected to.By default
mongoDB
looks for a file named .mongorc.js
in your home directory. So create a file ~/.mongorc.js
and put a content like below inside of it:print("Hello! Welcome to Alireza company :)");
print("Your database is set to: " + db);
That's it! Save the file and run
mongo
in your terminal, the output should be similar to the following:$ mongo
MongoDB shell version v3.6.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.2
Your database is set to: test
Hello! Welcome to Alireza company :)
>
In order to disable dangerous functionalities:
var no = function() { print("oops! You are not allowed to drop anything!!");};
// Prevent dropping databases
db.dropDatabase = DB.prototype.dropDatabase = no;
// Prevent dropping collections
DBCollection.prototype.drop =no;
// Prevent dropping indexes
DBCollection.prototype.dropIndex = no;
Sample output:
> db.dropDatabase('bi')
oops! You are not allowed to drop anything!!.
NOTE:
You can disable loading your .mongorc.js
by using the --norc
option when starting the shell.#mongodb #mongo #shell #mongorc
In order to connect to
Sample usages:
Read full details here:
- http://api.mongodb.com/python/current/examples/high_availability.html#connecting-to-a-replica-set
#database #mongodb #mongo #replica_set #replication #pymongo #arbiter #master #primary #slave
MongoDB replica set
in Python
you can give all server node addersses to MongoClient
. Addresses passed to MongoClient()
are called the seeds. As long as at least one of the seeds is online, MongoClient
discovers all the members in the replica set, and determines which is the current primary and which are secondaries or arbiters.Sample usages:
>>> MongoClient('localhost', replicaset='foo')
MongoClient(host=['localhost:27017'], replicaset='foo', ...)
>>> MongoClient('localhost:27018', replicaset='foo')
MongoClient(['localhost:27018'], replicaset='foo', ...)
>>> MongoClient('localhost', 27019, replicaset='foo')
MongoClient(['localhost:27019'], replicaset='foo', ...)
>>> MongoClient('mongodb://localhost:27017,localhost:27018/?replicaSet=foo')
MongoClient(['localhost:27017', 'localhost:27018'], replicaset='foo', ...)
Read full details here:
- http://api.mongodb.com/python/current/examples/high_availability.html#connecting-to-a-replica-set
#database #mongodb #mongo #replica_set #replication #pymongo #arbiter #master #primary #slave
Secondary Reads
By default an instance of MongoClient sends queries to the primary member of the replica set. To use secondaries for queries we have to change the read preference:
>>> client = MongoClient(
... 'localhost:27017',
... replicaSet='foo',
... readPreference='secondaryPreferred')
>>> client.read_preference
SecondaryPreferred(tag_sets=None)
Now all queries will be sent to the secondary members of the set. If there are no secondary members the primary will be used as a fallback. If you have queries you would prefer to never send to the primary you can specify that using the secondary read preference.
#mongodb #replica_set #replication #secondary #slave #pymongo
Today we had hundreds of connections to
Now you just need to send
Create as many object as you want from
#mongodb #mongo #singleton #design_pattern #__new__
MongoDB
that compared to previous days was so high and one of our modules initiating the connections every time a new request was initiated. To address the issue, I used a singleton design pattern to prevent creating many connections. Although pymongo
has its own connection pool, we reduced the connections by 40 to 60!Singleton
design pattern makes sure to only create a new object from your class only ONCE:class Singleton(object):
_instance = None
def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = object.__new__(class_, *args, **kwargs)
return class_._instance
__new__
will be called automatically when a new instance of the class is initiated. Here we check whether _instance
is set or not, in case it is set we will return the old instance and will not create a new object.Now you just need to send
Singleton
as parent class to your classes:class MongoConnnection(Singleton):
def __init__(self):
pass
def get_connection(self):
pass
Create as many object as you want from
MongoConnnection
. And now when you print the initiated class you will see one memory address not different ones.#mongodb #mongo #singleton #design_pattern #__new__
How to determine fragmentation in MongoDB?
#mongodb #mongo #stats #fragmentation
use mydb
var s = db.my_collection.stats()
var frag = s.storageSize / (s.size + s.totalIndexSize)
NOTE:
A frag value larger than 1 indicates some level of fragmentation.#mongodb #mongo #stats #fragmentation
In order to check the filesystem of the partition and where that partition maps to that address use:
#linux #mount #ext4 #nfs #filesystem #partition
mount -lIt will show for example that you have a partition that mounted by
NFS
or that a partition is ext4
and so on:/dev/mapper/vg-var on /var type ext4 (rw,relatime,stripe=384,data=ordered)
#linux #mount #ext4 #nfs #filesystem #partition
For optimal performance in terms of the storage layer, use disks backed by
Avoid
#mongodb #mongo #disk #raid #SSD
RAID-10
. RAID-5
and RAID-6
do not typically provide sufficient performance to support a MongoDB deployment.Avoid
RAID-0
with MongoDB deployments. While RAID-0
provides good write performance, it also provides limited availability and can lead to reduced performance on read operations, particularly when using Amazon’s EBS volumes.#mongodb #mongo #disk #raid #SSD
How to detect unused indexes on
#mongodb #mongo #index #unused_indexes
MongoDB
collections?db.your_collection.aggregate([{$indexStats:{}}]).pretty()
ops
displays operations per second on a specific index. If ops is very low compared to other indexes you can drop the index.#mongodb #mongo #index #unused_indexes
Tech C**P
#mongodb #backlog #road_map
The good news is
MongoDB
will support transactions from version 4.0 like any other DBMS systems like MySQL, Oracle,...OnePHP
a super simple and super lightweight PHP framework
that works in just a few lines:// index.php
require_once('src/OnePHP/one_framework.php');
$app = new \OnePHP\App();
$app->get('/:name',function( $name ) use ( $app ){//Action
echo "Hello $name";
});
$app->listen();
To install using composer:
composer create-project julces/oneframework
Why
OnePHP
:1- Restful Routes
2- Easy and clean (GET, POST, PUT, DELETE...) Requests management
3- Restful Response with HTTP Status Code and custom Headers
4- PHP native Views
5- No dependencies, add extra libraries only when you need it.
#php #framework #onePHP
If you have a dedicated server only for
Set the readahead setting to 0 regardless of storage media type (spinning, SSD, etc.).
Setting a higher readahead benefits sequential I/O operations. However, since
In general, set the readahead setting to 0 unless testing shows a measurable, repeatable, and reliable benefit in a higher readahead value. MongoDB Professional Support can provide advice and guidance on non-zero readahead configurations.
Ensure that readahead settings for the block devices that store the database files are appropriate. For random access use patterns, set low readahead values. A readahead of 32 (16 kB) often works well.
For a standard block device, you can run
#mongodb #mongo #ra #readahead #mmapv1 #wiredtiger
MongoDB
database I would I highly recommend to set block size as mongo suggested for their engines.For the WiredTiger storage engine:
Set the readahead setting to 0 regardless of storage media type (spinning, SSD, etc.).
Setting a higher readahead benefits sequential I/O operations. However, since
MongoDB
disk access patterns are generally random, setting a higher readahead provides limited benefit or performance degradation. As such, for most workloads, a readahead of 0 provides optimal MongoDB
performance.In general, set the readahead setting to 0 unless testing shows a measurable, repeatable, and reliable benefit in a higher readahead value. MongoDB Professional Support can provide advice and guidance on non-zero readahead configurations.
For the MMAPv1 storage engine:
Ensure that readahead settings for the block devices that store the database files are appropriate. For random access use patterns, set low readahead values. A readahead of 32 (16 kB) often works well.
For a standard block device, you can run
sudo blockdev --report
to get the readahead settings and sudo blockdev --setra <value> <device>
to change the readahead settings. Refer to your specific operating system manual for more information.#mongodb #mongo #ra #readahead #mmapv1 #wiredtiger
In
This is a nice way to swap variables without using a third variable.
#python #swap #swap_variables
python
in order to swap variables you can use unpack feature:>>> a, b = 1, 2
>>> a, b = b, a
>>> a, b
(2, 1)
This is a nice way to swap variables without using a third variable.
#python #swap #swap_variables
Extended unpacking (`Python 3 Only`):
This way first and last list element will be unpacked into
#python #python3 #unpack #extended_unpack
>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5
This way first and last list element will be unpacked into
a
and c
and other values will be inside of b
variable.#python #python3 #unpack #extended_unpack