【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update,mongodboperation

來源:互聯網
上載者:User

【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update,mongodboperation
1, Insert

MongoDB is database storing document object, the type of which is called Bson.(like JSON);

Example:  // document defination 

 

 

Now after using command[db.posts.insert(doc)], you will insert record successfully if seeing the 

The following picture.

 

2. Query 

One of the fundamental functions of MongoDB is to support query dynamically, which is the same as the troditional relational database, but more effiecieny than that. 

2.1 Query Expression Ojbects:

Query expression objects document is also a bson-structure document. For example, we could use the following the command to find all the record in the collections:

 

2.2 Query Item

In addition to Query expression object, Mongodb still support extra argument items. For example, you may only wanna return some certain fields. For example:

Example 1: return all the fields expect for tags;

 

Example 2: return dall fields expect for comments and tags=”albert”

 

Example 3: return the only field ’name’ and gender=’male’


3. Remove 

Removing operation is used to remove records from the collections.Example:

 


Advice: It’s better to use _id as condition when executing the remove operation/

Attention: In some conditions, when you are ready to remove one record, maybe in the meantime the update operation is updating this record which makes the reomve operation failed. As for this case, you could add the $atomic field to avoid this case. For example:


4. Update4.1 Grammar 


Argument Description:

Criteria: the object used to set query conditions

Objnew: Object used to set update content. 

Upsert: if record exists, it will update it. Else insert a new record.

Multi: if multi matches conditoin, it will update all the records. 

 Attention: By fault, mongoDB will update the first record that matches the query condition.

 

 

 

 


David Mytton為何從MySQL遷移到MongoDB資料庫

” 從中感到了作者的歡喜和憂愁,有翻譯不妥或理解不到位的,還請指正:)1,David為什麼要遷移?原文如下:寫道The problem we encountered was administrative. We wanted to scale using replication but found that MySQL had a hard time keeping up, especially with the initial sync. As such, backups became an issue, but we solved that. However, scaling MySQL onto multiple clustered servers as we plan to do in the future is difficult. You either do this through replication but that is only really suited to read-heavy applications; or using MySQL cluster. The cluster looks very good but I have read about some problems with it and was unsure of it’s suitability for our needs.看上去大概的意思是說:我們遇到了管理上的麻煩,雖然我們解決了備份問題。我們試圖通過MySql叢集解決,叢集看上去很好但對於一個大量寫應用來說卻遇到了困難,同時我們也不確定叢集是否適應我們的需求。於是David選擇更換MySQL,選擇了MongoDB。2、為什麼選擇MongonDB?寫道Very easy to install.PHP module available.Very easy replication, including master-master support. In testing this caught up with our live DB very quickly and stayed in sync without difficulty.Automated sharding being developed.Good documentation. 我想最重要的一點應該是:Very easy replication, including master-master support. In testing this caught up with our live DB very quickly and stayed in sync without difficulty. 非常容易的資料拷貝並且快速、一致。3、移植MongonDB後的問題。Schema-less:寫道Schema-lessThis means things are much more flexible for future structure changes but it also means that every row records the field names. We had relatively long, descriptive names in MySQL such as timeAdded or valueCached. For a small number of rows, this extra storage onl......餘下全文>>
 
David Mytton為何從MySQL遷移到MongoDB資料庫

” 從中感到了作者的歡喜和憂愁,有翻譯不妥或理解不到位的,還請指正:)1,David為什麼要遷移?原文如下:寫道The problem we encountered was administrative. We wanted to scale using replication but found that MySQL had a hard time keeping up, especially with the initial sync. As such, backups became an issue, but we solved that. However, scaling MySQL onto multiple clustered servers as we plan to do in the future is difficult. You either do this through replication but that is only really suited to read-heavy applications; or using MySQL cluster. The cluster looks very good but I have read about some problems with it and was unsure of it’s suitability for our needs.看上去大概的意思是說:我們遇到了管理上的麻煩,雖然我們解決了備份問題。我們試圖通過MySql叢集解決,叢集看上去很好但對於一個大量寫應用來說卻遇到了困難,同時我們也不確定叢集是否適應我們的需求。於是David選擇更換MySQL,選擇了MongoDB。2、為什麼選擇MongonDB?寫道Very easy to install.PHP module available.Very easy replication, including master-master support. In testing this caught up with our live DB very quickly and stayed in sync without difficulty.Automated sharding being developed.Good documentation. 我想最重要的一點應該是:Very easy replication, including master-master support. In testing this caught up with our live DB very quickly and stayed in sync without difficulty. 非常容易的資料拷貝並且快速、一致。3、移植MongonDB後的問題。Schema-less:寫道Schema-lessThis means things are much more flexible for future structure changes but it also means that every row records the field names. We had relatively long, descriptive names in MySQL such as timeAdded or valueCached. For a small number of rows, this extra storage onl......餘下全文>>
 

相關文章

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.