Basic use of Mongodb

Source: Internet
Author: User

One, cmd connection MongoDB service

Enter the bin directory of MongoDB: [d:\mongodb3.2.5\bin]$ MONGO 127.0.0.1:27017

Common query:

Show DBS View all databases

Use DbName into the current database

Show tables View collections under this database

Second, curd operation

Inquire:

1. Check all: Db.mydb2_collection.find ();

2. Query A: Db.mydb2_collection.findOne ();

Insert: Db.mydb2_collection.insert ({m:1});

Modified: Db.mydb_collection.update ({x:1},{x:999});

Delete: Db.mydb_collection.remove ({c:2});

Third, index

Pros: Can speed up all related queries
Cons: Increase disk space and reduce write performance

Index Type:

ID index: auto-created
Single-Key index:
Multi-key index: Db.mydb2_collection.insert ({x:[1,2,3,4,5]});
Composite index: When we do not know the condition of the query, we need to build a composite index
Db.mydb2_collection.ensureIndex ({x:1,y:1}); Create
Db.mydb2_collection.find ({x:1,y:2}); Use
Expiration index: 1. Index 2 that expires after a certain period of time, and the corresponding data is deleted after the index expires
Usage scenarios: Suitable for storing data that is invalidated after some time such as user login information, stored logs
How to create:
Note: The field stored in the expired index must be of the specified time type, must be a isodate or isodate array, cannot use a timestamp, or it cannot be deleted automatically.
If it is an array, it is deleted by the minimum time
Expired index is not a composite index
The deletion time is imprecise (the removal process is a process operation that the daemon does not perform once in 60 seconds, and the delete operation itself takes time) the minimum difference of 60 seconds
Full-Text indexing: Creating full-text searchable indexes on strings and string arrays
Insert data: Db.mydb2_collection.insert ({arrticle: "AA bb cc gg hh"});
Query: Db.mydb2_collection.find ({$text: {$search: "AA"}});
Db.mydb2_collection.find ({$text: {$search: "AA bb ee"}}); Multi-keyword or relationship
Db.mydb2_collection.find ({$text: {$search: "AA Bb-cc"}}); does not contain CC
Db.mydb2_collection.find ({$text: {$search: "\" aa\ "\" bb\ "\" Cc\ "}});//multi-keyword and relationship
Full-text index similarity:
Query: Db.mydb2_collection.find ({$text: {$search: "AA bb"}},{score:{$meta: "Textscore"}});
Db.mydb2_collection.find ({$text: {$search: "AA bb"}},{score:{$meta: "Textscore"}}). sort ({score:{$meta: "Textscore"} Sort

Properties of the index:
Name property
Specify the name to create the index: Db.mydb2_collection.ensureIndex ({c:1,y:1},{name: "Normal_index"});
Delete Index by name: Db.mydb2_collection.dropIndex ("Normal_index");
Uniqueness of Unique indexes:
Db.mydb2_collection.ensureIndex ({m:1,n:1},{unique:true})
Function: Do not insert if present when adding, otherwise insert
Sparsity: Sparse
Created: Db.mydb2_collection.ensureIndex ({m:1},{sparse:true});
Query Force index: db.mydb2_collection.find ({m:{$exists: false}}). Hint ("m_1");
Location Index:
2d:
Create 2d Flat index: Db.mydb_location.ensureIndex ({w: "2d"});
Location representation: Latitude [longitude, latitude]
Range of values: Longitude: [-180,180] latitude [ -90,90]
Insert: Db.mydb_location.insert ({w:[2,1]});
Near query:
Check the nearest point to []: Db.mydb_location.find ({w:{$near: []});
Search for points similar to [10]: Db.mydb_location.find ({w:{$near: [All], $maxDistance: 10}});
Shape Query $geoWithin:
Rectangle $box: Db.mydb_location.find ({w:{$geoWithin: {$box: [[0,0],[2,2]}}});
Round $center:db.mydb_location.find ({w:{$geoWithin: {$center: [[[0,0],2]}}});
Polygon $polygon:db.mydb_location.find ({w:{$geoWithin: {$polygon: [[[0,0],[2,2],[0,2]]}}});
Geonear query:
Db.runcommand ({geonear: "Mydb_location", near:[1,2],maxdistance:3,num:1});
2dsphere Index Spherical Location:

Iv. Security Mechanisms

MongoDB Security mechanism:
1. Physical separation (safest): unrealistic
2. Network isolation
3. Firewall isolation: Give fixed IP access rights
4. User name and password

Create user
Db.createuser ({User: "Jalja", pwd: "Jalja", Roles:[{role: "Useradmin", DB: "MyDB"},{role: "read", DB: "Test"}]});

Permissions: Read, ReadWrite, DbAdmin, Dbowner, useradmin

Add Auth =true enable authentication in mongo.conf

Connect using username and password: MONGO 127.0.0.1:27017-u jalja-p Jalja

The role of MongoDB
1. Database roles
2. Cluster role
3. Data Backup role
4. Other special privileges

Basic use of Mongodb

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.