Mongodb quick start guide and docker compose quick experience
Advantages of mongodb over RDBMS
- 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.
- The structure of a single object is clear.
- There is no complex join.
- Deep query capability. Mongodb supports dynamic query of documents using document based query language which is almost as powerful as SQL.
- SQL tuning (optimization)
- Easy to expand
- There is no need to convert / map application objects to database objects.
- Use internal memory to store (windowed) working sets, allowing faster access to data
RDBMS: relational database management system
Why use mongodb
- Document oriented storage, bson format storage, namely binary JSON
- Single key index, composite index, multi key index, geospatial index, full-text index and hash index
- Mongodb for high availability master slave replication
- Auto sharding
- Rich queries
- Fast in place updates: most updates do not require new space
- Mongodb professional support map / reduce support
- Gridfs: cluster file support of various sizes
Where to use mongodb
- big data
- Content management and delivery
- Mobile and social infrastructure
- User data management
- 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
Fill in the docker above- compose.yml Corresponding parameters
Test connection, test successful
If you don’t show, go
Navicat = = > menu = = > show = = > shows hidden items on the hook
Just restart Navicat
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()
//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()
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.
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()
//Change all qty greater than 15 to 15
db.products.updateMany(
{
qty: {
$gt: 15
}
},
{
$set: {
"qty": 15
}
}
);
- 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()
//Delete the
db.products.deleteOne( { "item" : "stampss" } );
//Batch delete those with qty less than 15
db.products.deleteMany( { "qty" : {$lt: 15} } );