MongoDB Database Learning Notes

Source: Internet
Author: User
Tags bulk insert


First, the MongoDB database of the increase and deletion check


Show databases
Show DBS//display database;

Show tables
Show collections//Chahi or set;

Use IMOOC//using or creating database Imooc;


Increase:
Use IMOOC
Db.imooc_collection.insert ({x:1})//To the collection name "Imooc_collection" Insert a single data "x:1";

Db.imooc_collection.insert ({x:2})//To the collection name "Imooc_collection" Insert a single data "X:2";
Db.imooc_collection.insert ({x:100,y:100,z:100})//Go to the collection name "Imooc_collection" to insert a multi-field single data;
Bulk INSERT Data "x:3-99" for (i=3;i<100;i++) Db.imooc_collection.insert ({x:i})//Toward the collection name "Imooc_collection";


By deleting:
Db.imooc_collection.remove ({c:2})//delete collection or table matching data (must pass parameter, otherwise error);
Db.imooc_collection.drop ()//delete collection or table;

Use IMOOC
Db.dropdatabase ()//delete database Imooc;


Change:
Db.imooc_collection.update ({x:1},{x:999})//Change the set name "Imooc_collection" in "X:1" to "x:999";
Db.imooc_collection.update ({z:100},{$set: {y:99}})//When looking for "z:100", only the Y field is updated as "y:99";
Db.imooc_collection.update ({y:100},{y:999},true)//When the data being found does not exist, insert a piece of data;
Db.imooc_collection.update ({c:1},{$set: {c:2}},false,true)//Bulk update of all matching data;

Note: MongoDB By default only updates the first data found, the purpose is to prevent update error operation;


Check:
Db.imooc_collection.find ()//query all data contents in the set name "Imooc_collection";
Db.imooc_collection.find (). Count ()//Statistics The number of all data contents in the set name "Imooc_collection";
Db.imooc_collection.find (). Skip (3)//query filter the first 3 data;
Db.imooc_collection.find (). Limit (10)//query limit displays only the first 10 data;
Db.imooc_collection.find (). Sort ({x:1})//query using "x" to sort;


Second, the index


Increase:
Db.collection.ensureIndex ()

By deleting:
Db.collection.dropIndex ("index name")

Change:

Check:
Db.collection.getIndexes ()

1._ID Index:

Use IMOOC
Db.table2.insert ({x:1})
Db.table2.getIndexes ()//_id index is a unique index that is automatically created by default when inserting data (the command is an index within the query collection);

2. Single Key index:

Db.table2.ensureIndex ({x:1})//Create a single-key index of "X:1", which is a single-key index when the query condition is a value;


3. Multi-key index:

Db.table2.insert ({x:[1,2,3,4,5]})//Insert an array, and then create an index that is a multi-key index;


4. Composite Index:

Db.table2.ensureIndex ({x:1,y:2})//The index created when the query condition is more than one is a composite index;


5. Expired index:

Db.table2.insert ({time:new Date ()})//Insert time: One piece of data for the current period;
Db.table2.ensureIndex ({time:1},{expireafterseconds:30})//Create an expired index with a key value of time and an expiration of 30;

Note: The data must be isodate or isodate array to be automatically deleted, every 60 seconds to perform the detection;


6. Full-Text indexing:

Db.table2.ensureIndex ({key: "text"})
Db.table2.ensureIndex ({key_1: "text", ke_2: "Text"})
Db.table2.ensureIndex ({"$**": "Text"})//Three ways to create a full-text index;

Db.table2.find ({$text: {$search: "AA"}})
Db.table2.find ({$text: {$search: "AA bb cc"}})
Db.table2.find ({$text: {$search: "AA Bb-cc"}})
Db.table2.find ({$text: {$search: "\" aa\ "\" Bb\ ""}})
Db.table2.find ({text:{$search: "AA bb"}},{score:{$meta: "Textscore"}}). sort ({score:{$meta: "Textscore"}})//Use full

Five ways to index the text;

Note: Full-text indexing can only specify one "text"; MongoDB temporarily does not support Chinese;

Index Important properties:
1. First name (name);
2. Uniqueness (unique);
3. sparsity (sparse);
4. Whether to delete periodically (expireafterseconds);


7. Location Index:

7.1 2d Index

7.2 2dsphere Index


Third, MongoDB security:


1. Open permission authentication;
2. Create a user;
3. Create user roles;



This article is from the "Square Hill" blog, please be sure to keep this source http://63638790.blog.51cto.com/513514/1650031

MongoDB Database Learning Notes

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.