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

时间: 2024-08-05 07:08:41

【MongoDB】The basic operation of Mongodb, Insert\Query\Delete\Update的相关文章

【MongoDB】The basic operation of Index in MongoDB

In the past four blogs, we attached importance to the index, including description and comparison with usage of index. Now in this blog, we will mainly focus on the basic operation of index. such query, remove ,repair and so on. 1. View Index getInde

【转载】8天学通MongoDB——第八天 驱动实践

作为系列的最后一篇,得要说说C#驱动对mongodb的操作,目前驱动有两种:官方驱动和samus驱动,不过我个人还是喜欢后者, 因为提供了丰富的linq操作,相当方便. 官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads.下载后,还提供了一个酷似msdn的帮助文档. samus驱动:https://github.com/samus/mongodb-csharp/downloads. 下面就具体看看samus驱动,https:

【转载】8天学通MongoDB——第三天 细说高级操作

今天跟大家分享一下mongodb中比较好玩的知识,主要包括:聚合,游标. 一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工具,它的使用跟我们C#里面的count使用简直一模一样. <2> distinct 这个操作相信大家也是非常熟悉的,指定了谁,谁就不能重复,直接上图. <3> group 在mongodb里面做group操作有点

【转载】8天学通MongoDB——第四天 索引操作

这些天项目改版,时间比较紧,博客也就没跟得上,还望大家见谅. 好,今天分享下mongodb中关于索引的基本操作,我们日常做开发都避免不了要对程序进行性能优化,而程序的操作无非就是CURD,通常我们 又会花费50%的时间在R上面,因为Read操作对用户来说是非常敏感的,处理不好就会被人唾弃,呵呵. 从算法上来说有5种经典的查找,具体的可以参见我的算法速成系列,这其中就包括我们今天所说的“索引查找”,如果大家对sqlserver比较了解 的话,相信索引查找能给我们带来什么样的性能提升吧. 我们首先插

【MongoDB】Serveral common command of MongoDb

In the recent days, since the overwork made me exhaused, on arrival to home I will go to bed, which leads to loss of blogs that should have been written in the past days. Now  I make a summay to all the commond of operating mongoDB. First, you should

【转】MySQL的Replace into 与Insert into on duplicate key update真正的不同之处

原文链接:http://www.jb51.net/article/47090.htm 今天听同事介绍oracle到mysql的数据migration,他用了Insert into ..... on duplicate key update ...,我当时就想怎么不用Replace呢,于是回来就仔细查了下,它们果然还是有区别的 看下面的例子吧: 1 Replace into ... 1.1 录入原始数据 mysql> use test; Database changed mysql>  mysq

【MongoDB】在windows平台下mongodb的分片集群(五)

本篇接着上面的四篇继续讲述在window平台下mongodb的分片集群搭建.在分片集群中也照样可以创建索引,创建索引的方式与在单独数据库中创建索引的方式一样.因此这不再多说.本篇主要聚焦在分片键的选取问题上. 分片键通俗来说就是分割海量数据的标记符. 如果更高效的划分海量数据往往依赖于分片键的选择. 分片键选得不好,应用程序就无法利用分片集群所提供的诸多优势.在这种情况下,查询和插入得系能都回显著下降. 一.低效的分片键 1.1 分布差 BSON对象ID是每个mongodb文档的默认主键.所有的

【MongoDB】在windows平台下mongodb的分片集群(三)

在上两篇博客中我们分别阐述了分片集群的工作原理以及搭建过程.在本篇博客中我们主要一起分析分片集群的测试结果: 首先看分片集群的各个状态,可以看的出复制集A和B都是正常的: 一.开启分片集合 开启一个数据库上的分片,这是对任何集合进行分片的先决条件. 假设测试数据库为slidetest. 分片集合定义注意:它看起来和索引定义有几分相似之处,尤其是有那个unique键.在对一个空集合进行分片时候,mongodb会在每个分片上创建一个与分片对应的索引.可以直接连上分片,运行getIndexs()进行验

【MongoDB】The Access control of mongodb

In this blog we mainly talk about the access control including limitation of ip, setting listen port and usage of username and password. In the official document, mongodb server start without the argument , so once the connection was created, the rem