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

來源:互聯網
上載者:User

標籤:mongodb

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.

 

 

 

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.