MongoDB database: Pipelines, user management, replica sets, etc.

Source: Internet
Author: User

Aggregation (aggregate): DB. Collection. aggregate ([{pipe: {expression}}])

Db. Collection. Aggregate ([
{Pipe 1:{expression 1}},
{Pipe 2:{expression 2}},
... ...
])

Pipeline 1 results as input to pipeline 2.

$group: grouping {$group: {
_id: ' $gender ',
count:{$sum: 1}
}}

$match: Filter (older than) {$match: {age:{$gt: 19}}}

$project: Projection (same as find second set of parameters, 1 display, 0 not shown) {$project: {_id:0,counter:1}}

$sort: Sort (same as sort, 1 ascending,-1 descending) {$sort: {_id:-1}}

$skip (Skip the number of clauses, same as Skip) {$skip: 2}

$limit (Get how many clauses, same as limit) {$limit: 2}

$unwind (You can split the array into a single document) {$unwind: ' $xxx '}

Index (1: Ascending index, 2: Descending index):d B. Collection. Ensureindex ({Property 1:1, property 2:1})

View the document index DB. Collection. GetIndex ()

Delete Index db. collection. Dropindex (' Index name ')

Role: Root is available only in the Admin database, super account
READ: Allows the user to read the specified database
ReadWrite: Allows the user to read and write to the specified database

To create a super Administrator:
Db.createuser ({User: ' admin ', pwd: ' 123 ', roles:[{role: ' Root ', db: ' Admin '}]})

To enable validation modification/etc/mongodb.conf:

Security
Authorization:enabled

or Auth=true.

To create a normal user with a super Administrator:
Db.createuser ({User: ' User1 ', pwd: ' 123 ', Roles:[{role: ' ReadWrite ', DB: ' Students '}]})

Login: Mongo-u user1-p 123--authenticationdatabase Students

Replica set: Data backup, etc.:
Mongod--bind_ip x.x.x.x--porx 27018--dbpath ~/desktop/t1--replset rs0
Mongod--bind_ip x.x.x.x--porx 27019--dbpath ~/desktop/t2--replset rs0

Connection MONGO--host x.x.x.x--port 27018

Initialize Primary server: >rs.initiate () View status Rs.status ()

Add replica set: Rs.add (' x.x.x.x:27019 ') ')
Delete Replica set: Rs.remove (' x.x.x.x:27019 ') ')

The read operation from the server needs to be set Rs.slaveok ()

Automatic master-Slave switchover: One server shuts down another automatically switches to the primary server.

Database backup: Mongodump-u user1-p 123--authenticationdatabase database name-d backup DATABASE-O storage location

Database recovery: Mongorestore-h server address-D database--dir backup database location to be restored

MongoDB database: Pipelines, user management, replica sets, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.