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

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. Update

4.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.

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值