Mongodb quick start guide and docker compose quick experience

Time:2020-11-24

Mongodb quick start guide and docker compose quick experience

Advantages of mongodb over RDBMS

  1. Schema less – mongodb is a document database in which a collection contains different documents. The number of fields, content, and document size may vary between documents.
  2. The structure of a single object is clear.
  3. There is no complex join.
  4. Deep query capability. Mongodb supports dynamic query of documents using document based query language which is almost as powerful as SQL.
  5. SQL tuning (optimization)
  6. Easy to expand
  7. There is no need to convert / map application objects to database objects.
  8. Use internal memory to store (windowed) working sets, allowing faster access to data

RDBMS: relational database management system

Why use mongodb

  1. Document oriented storage, bson format storage, namely binary JSON
  2. Single key index, composite index, multi key index, geospatial index, full-text index and hash index
  3. Mongodb for high availability master slave replication
  4. Auto sharding
  5. Rich queries
  6. Fast in place updates: most updates do not require new space
  7. Mongodb professional support map / reduce support
  8. Gridfs: cluster file support of various sizes

Where to use mongodb

  1. big data
  2. Content management and delivery
  3. Mobile and social infrastructure
  4. User data management
  5. Data center

Docker compose quick start mongodb

docker-compose.yml

version: '3'
    services:
      mongodb:
        image:  mongo:4.2.6 #  Mirroring: versions
        container_name: mongo_db
        environment:
          - MONGO_ INITDB_ Database = default database
          - MONGO_ INITDB_ ROOT_ Username = your root administrator name
          - MONGO_ INITDB_ ROOT_ Password = your root administrator name and password
        volumes:
          - ./mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
          - ./mongo/mongo-volume:/data/db
        ports:
          - "27017-27019:27017-27019"
        restart: always

init-mongo.js

// db.getSiblingDB () is equivalent to use admin;
db.getSiblingDB('admin')
    .createUser({
        user: 'user',
        pwd: 'user',
        roles: ['readWrite']
});

Then execute the command

docker-compose up -d

Navicat premium connectivity

Mongodb quick start guide and docker compose quick experience
Fill in the docker above- compose.yml Corresponding parameters
Test connection, test successful
Mongodb quick start guide and docker compose quick experience
If you don’t show, go

Navicat = = > menu = = > show = = > shows hidden items on the hook

Just restart Navicat

Mongodb quick start guide and docker compose quick experience

Open a collection in the lower right corner to view it

  • Grid view
  • Tree view
  • JSON view

Add, delete, modify and search

1. Create operation

  • db.collection.insertOne()
  • db.collection.insertMany()

Mongodb quick start guide and docker compose quick experience

//Insert a set with the collection name products and merge the inserted data (if there is no set, a new one will be created)
db.products.insertOne({
    item: "card",
    qty: 15
});

//Batch insert data
db.products.insertMany([{
    item: "card1",
    qty: 16
}, {
    item: "card2",
    qty: 17
}, {
    item: "envelope",
    qty: 20
}, {
    item: "stamps",
    qty: 30
}]);

2. Query operation

  • db.collection.find()

Mongodb quick start guide and docker compose quick experience

db.products.find({ qty: 15 }).limit(1)

3. Update operation

  • db.collection.updateOne()

db.collection.updateOne(filter,update,options)

Finds the first document that matches the filter and applies the specified update modification.

Mongodb quick start guide and docker compose quick experience

db.products.find({
    item: "stamps"
}).limit(1);

//Find the item with stamp and modify its qty to 60
db.products.updateOne(
    {
        "item": "stamps"
    },
    {
        $set: {
            "qty": 60
        }
    }
);

db.products.find({
    item: "stamps"
}).limit(1);
  • db.collection.updateMany()

Mongodb quick start guide and docker compose quick experience

//Change all qty greater than 15 to 15
db.products.updateMany(
    {
        qty: {
            $gt: 15
        }
    },
    {
        $set: {
            "qty": 15
        }
    }
);

Mongodb quick start guide and docker compose quick experience

  • db.collection.replaceOne()

Replace the first matching document in the collection that matches the filter with a replacement document.

//Replace item as stamps in the set products 
 db.products.replaceOne(
    { "item" : "stamps" },
    { "item" : "stampss", "Borough" : "Manhattan" }
 );

4. Delete operation

  • db.collection.deleteOne()
  • db.collection.deleteMany()

Mongodb quick start guide and docker compose quick experience

//Delete the
 db.products.deleteOne( { "item" : "stampss" } );
 //Batch delete those with qty less than 15
 db.products.deleteMany( { "qty" : {$lt: 15} } );

Keep updating connections