Mongodb Operators Cheat Sheet

broken image


MongoDB is a popular NoSQL database that allows unauthenticated access by default.

Regardless of the user's authentication database, Mongo always stores user information in admin.

MongoDB stores all user information, including name, password, and the user's authentication database, in the system.users collection in the admin database.

You just clipped your first slide! Clipping is a handy way to collect important slides you want to go back to later. Now customize the name of a clipboard to store your clips. Expression Operators¶. These expression operators are available to construct expressions for use in the aggregation pipeline stages. Operator expressions are similar to functions that take arguments. In general, these expressions take an array of arguments and have the following form. Vim cheat sheet; SQL Cheat Sheet; Git Ftp; Recent Comments. Admin on TCET Session Data; Sandeep prajapati on TCET Session Data; v on Core Java Assignment; Craig Rothwell on Angular 4 http post with php; Glory Pachnanda on Basic Programs; Archives. April 2021; March 2021; February 2021; November 2020; September 2020; August 2020; June 2020.

See centralized-user-data and system-users-collection.

When you create a user and grant that user access to a single database (aka their authentication database) then that information can only be stored in the admin Descargar gp5 tabs. database.

So, it's not really a question of 'best practice'; storing user details in admin is MongoDB's choice, as implemented by their user management commands.

Update in response to this comment:

Ok, so the users are always located in the admin db, but I may also add 'duplicates' to the other dbs? Maybe the question should be whether there any advantage in adding users to the other 'non admin' dbs?

If you intend to have a single user with access to multiple databases then create a single user with roles in each of those databases rather than creating that user multiple times i.e. once in each of those databases. For example:

Create initial admin user

Sharded Cluster with enforced authentication

Create:

  • a cluster-wide admin user
  • a replica set specific admin user

Cluster-wide Admin

Replica Set Admin (a.k.a shard local)

Enable authentication in the mongos configuration

Connect to all replica set member nodes

Authenticate and check that the admin users exist

Log in

Rename collections

Copy Collection

Check replication set status

Backup/Restore

Documents

Admin commands

TLS 1.2 for Mongo Routers

To protect your application's database connection enable TLS on the mongo routers as follows. Note that your mongo driver configuration needs to trust the CA certificate and enable transport encryption with ssl=true.

Rolling Update/Cluster Patching

Maintenance (startup in reverse order):

Replication Concept

  1. write operations go to the primary node
  2. all changes are recorded into operations log
  3. asynchronous replication to secondary
  4. secondaries copy the primary oplog
  5. secondary can use sync source secondary*
  • automatic failover on primary failure

*settings.chainingAllowed (true by default)

Replica set oplog

  • special capped collection that keeps a rolling record of all operations that modify the data stored in the databases
  • idempotent
  • default oplog size (for Unix and Windows systems):

    Storage EngineDefault Oplog SizeLower BoundUpper Bound
    In-memory5% of physical memory50MB50GB
    WiredTiger5% of free disk space990MB50GB
    MMAPv15% of free disk space990MB50GB

Deployment

  • start each server with config options for replSet
    /usr/bin/mongod --replSet 'myRepl'
  • initiate the replica set on one node - rs.initialize()
  • verify the configuration - rs.conf()
  • add the rest of the nodes - rs.add() on the primary node
    rs.add('node2:27017') , rs.add('node3:27017')
  • check the status of the replica set - rs.status()

Sharding

Components

  • shard/replica set - subset of the sharded data
  • config servers - metadata and config settings
  • mongos - query router, cluster interface
    sh.addShard('shardName')

Shards

  • contains subset of sharded data
  • replica set for redundancy and HA with odd number of voting members
  • primary shard
  • don't shard collections if dataset fits into single server
  • –shardsvr in config file (port 27018)
  • every xxx has a primary shard per database
  • all non-shared collections will reside on primary shard
Shard keys (and limitations)
  • shard keys are immutable with max size of 512 bytes (can not be updated/changed)
  • must be ascending indexed key or indexed compound keys that exists in every document in the collection
  • cannot be multikey index, a text index or a geospatial index
  • update operations that affect a single document must include the shard key or the _id field
  • no option for sharding if unique indexes on other fields exist
  • no option for second unique index if the shard key is unique index
  • ranged sharding may not distribute the data evenly
  • hashed sharding distributes the data randomly

Config servers

  • config servers as replica set (only 3.4)
  • stores the metadata for sharded cluster in config database
  • authentication configuration information in admin database
  • holds balancer on Primary node (>= 3.4)
  • –configsvr in config file (port 27019)

mongos

  • caching metadata from config servers
  • routes queries to shards
  • no persistent state
  • updates cache on metadata changes
  • holds balancer (mongodb <= 3.2)
  • mongos version 3.4 can not connect to earlier mongod version

Sharding collection

StepCommand
Enable sharding on databasesh.enableSharding('users')
Shard collectionsh.shardCollection('users.history', { user_id : 1 } )
Shard key - indexed key that exists in every documentrange based
sh.shardCollection('users.history', { user_id : 1 } )
hashed based
sh.shardCollection( 'users.history', { user_id • 'hashed' } )
outputmarkdown source

to start server
mongod

Sheet

to start shell
mongo

show dbs;

create/use database
use db_name;

show collections;

db.createCollection('students');

db.students.drop();

show current database;
db;

drop database
db.dropDatabase();

insertOne
db.students.insertOne({_id: 1, name: 'shailesh'});

insertMany
db.students.insertMany([{_id: 2, name: 'bagde'}, {_id: 3, name: 'sheldon'}]);

select all records
db.students.find();

Mosfet vdsat calculation. select with projection
db.students.find({},{_id: 0});
db.students.find({},{name:0});

exists operator
db.students.find({age: {$exists: false}});

COMPARISION OPERATORS
in operator
db.students.find({_id: {$in: [2,3]}});

not equal operator
db.students.find({_id: {$ne: 2}});

equal operator
db.students.find({_id: {$eq: 2}});

less than $lt

Mongodb Cheat Sheet Pdf

less than equal $lte

greather than $gt

greater than equal $gte

LOGICAL OPERATORS

And in mongodb

to start shell
mongo

show dbs;

create/use database
use db_name;

show collections;

db.createCollection('students');

db.students.drop();

show current database;
db;

drop database
db.dropDatabase();

insertOne
db.students.insertOne({_id: 1, name: 'shailesh'});

insertMany
db.students.insertMany([{_id: 2, name: 'bagde'}, {_id: 3, name: 'sheldon'}]);

select all records
db.students.find();

Mosfet vdsat calculation. select with projection
db.students.find({},{_id: 0});
db.students.find({},{name:0});

exists operator
db.students.find({age: {$exists: false}});

COMPARISION OPERATORS
in operator
db.students.find({_id: {$in: [2,3]}});

not equal operator
db.students.find({_id: {$ne: 2}});

equal operator
db.students.find({_id: {$eq: 2}});

less than $lt

Mongodb Cheat Sheet Pdf

less than equal $lte

greather than $gt

greater than equal $gte

LOGICAL OPERATORS

$and
db.students.find({$and : [{_id : {$gt:1}}, {_id: {$lt: 4}}]})

$or
db.students.find({$or : [{name : 'bagde'}, {name: 'shailesh'}]})

like case insensitive
db.students.find({name: /h/i})

exact match
db.students.find({name: /^BagDe$/i})

UPDATE
db.students.updateOne({_id: 1}, {$set : {age: 23}});
db.students.updateMany({age : {$exists: false}}, { $set : {age : 25}})

Mongodb Array Operators

db.students.updateMany({}, {$set: {address: {city: 'Mumbai', state: 'MH'}}})
db.students.updateOne({_id: 4}, { $set : {address : {city: 'Nagpur'}}});

Unset/Delete Fields
db.students.updateMany({}, {$unset : {city: ''}});

search within sub document
db.students.find({address : { city : 'Nagpur'}}).pretty();

Delete Records
db.students.deleteOne({ $or : [{ age : {$lt : 25}}, {name : 'sheldon'}] })

ORDER BY
db.students.find().sort({name: -1})

Mongodb Operators Cheat Sheet Excel

LIMIT
db.students.find().sort({name: -1}).limit(1)





broken image