`

MongoDB 操作命令

阅读更多

MongoDB基本操作命令

11.id key
   系统自动生成也可以手动指定
   系统自动生成:BSON类型的,12个byte的2进制数据,
   12 个byte:4byte的时间戳timestamp,
              3-byte machine id,
              2-byte process id,
              3-byte counter
    
    索引被存储在system.indexes 集合里
    
12.sort 函数 Sort, Limit, and Skip    
    sort 对返回结果进行排序
    db.media.find().sort( { Title: 1 })
    1为升序,-1为降序 默认为升序
   
    If you specify a key for sorting that does not exist, the values will be returned in their ascending insertion order.
    limit 限制结果返回的数量
    db.media.find().limit( 10 )
   
  13.natural order,Capped Collections,$natural
       natural order 自然语序,数据在集合里按照插入的顺序,但是没有任何担保他们在集合里是完全按照插入的顺序,因为中途涉及到更改,删除
       Capped Collections  完全是按照插入的顺序排列的,有担保的,有固定的大小,一旦达到了峰值,那么最老的数据将被删除,新数据将被加到末尾
                          创建的时候必须明确指出createCollection 函数,必须带一个参数指定集合的大小 例如:
                          db.createCollection("audit", {capped:true, size:20480}){ "ok" : 1 } 创建集合audit,大小为20480
                          使用max 函数限制Capped collection 集合的 条目数量也就是mysql中的 行数
                          note:Capped Collections 集合里的文档能被更新,但是不能超过集合 大小,不能删除,可以drop整个集合
                          可使用validate() 函数查看集合剩余的条目数 也即行数
                         
       $natural           反转 capped collection 集合的顺序,db.audit.find().sort( { $natural: -1 } ).limit ( 10 )
   
  14.检索一个文档 (Retrieving a Single Document)
     findOne()
    
  15.聚合命令(Using the Aggregation Commands)  
     (1)count
     (2)distinct
     (3)group
    
      --count 返回集合的文档个数 类似于sql 语句中的count 
      --distinct 去重 unique 特性
      --group group函数有三个参数  key, initial, 和 reduce.
        (1)key 根据哪一个文档里面的属性进行分组.相当于mysql中的 列名。
        (2)initial 返回分类结果,默认为0.只代表返回所有计算的真实结果
        (3)reduce 组合相似的结果在一起,有两个额外的参数 prev 和items
       
        除了这3基本参数,还有额外的三个其他的参数 也能被使用
        In addition to the key, initial, and reduce parameters, you can specify three more optional
parameters:

keyf: You can use this parameter to replace the key parameter if you do not wish to group the
results on an existing key in your documents. Instead, you would group them using another
function you design that specifies how to do grouping.
cond: You can use this parameter to specify an additional statement that must be true before a
document will be grouped. You can use this much as you use the find() query to search for
documents in your collection. If this parameter isn’t set (the default), then all documents in the
collection will be checked.
   finalize: You can use this parameter to specify a function you want to execute before the final
results are returned. For instance, you might calculate an average or perform a count and
include this information in the results.
                   
         Note: The group() function does not currently work in sharded environments. For these, you should use the
mapreduce() function instead. Also, the resulting output cannot contain more than 10,000 keys in all with the
group() function, or an exception will be raised. This too, can be bypassed by using mapreduce().                    

16.条件查询
   $gt 大于 (不包括)
   $lt 小于 (不包括)
   $gte 大于等于
   $lte 小于等于
    > db.media.find ( { Released : {$gt : 2000} }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
> db.media.find ( { Released : {$gte : 1999 } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c43694bc603000000007ed1"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
> db.media.find ( { Released : {$lt : 1999 } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c436969c603000000007ed2"), "Type" : "DVD", "Title" : "Blade Runner",
"Released" : 1982 }
> db.media.find( {Released : {$gte: 1990, $lt : 2010}}, { "Cast" : 0 })
{ "_id" : ObjectId("4c43694bc603000000007ed1"), "Type" : "DVD", "Title" :
"Matrix, The", "Released" : 1999 }

17.检索所有的文档除了被指定的
   $ne (not equals)
    > db.media.find( { Type : "Book", Author: {$ne : "Plugge, Eelco"})
18.基本操作命令
     (1)$in 相当于sql 中的in 
     > db.media.find( {Released : {$in : ["1999","2008","2009"] } }, { "Cast" : 0 } )
{ "_id" : ObjectId("4c43694bc603000000007ed1"), "Type" : "DVD", "Title" : "Matrix, The",
"Released" : 1999 }
     (2)$nin 相当于not in
     > db.media.find( {Released : {$nin : ["1999","2008","2009"] },Type : "DVD" },
{ "Cast" : 0 } )
{ "_id" : ObjectId("4c436969c603000000007ed2"), "Type" : "DVD", "Title" :
"Blade Runner", "Released" : 1982 }
{ "_id" : ObjectId("4c4369a3c603000000007ed3"), "Type" : "DVD", "Title" :
"Toy Story 3", "Released" : 2010 }
(3)$all 必须都匹配
> db.media.find ( { Released : {$all : ["2010","2009"] } }, { "Cast" : 0 } )
(4)$or
(5)$slice 限制数组的个数 可以根据下表的范围检索 是 limit 和skip的结合函数,但是 limit和skip 不能操作数组
(6)$mod 奇数,偶数
    Released : { $mod: [2,0] }
    Released : { $mod: [2,1] }
(7)$size 匹配指定数量的数组集合  
(8)$exists  Author : {$exists : true }  Author : {$exists : false }
(9)$type
(10)$elemMatch 匹配整个数组
(11)$not 和 $elemMatch 相反
(12)javaScript 函数
     f = function() { return this.Released < 1995;}
         db.media.find(f)
     (13)正则表达式  
    
  19.更新(update) 
     update() criteria, objNew, upsert, and multi 
    
     (1)The criteria argument lets you specify the query that selects the record you want to update.
     (2)You use the objNew argument to specify the updated information;
     (3)The upsert argument lets you specify whether the update should be an upsert,An upsert argument
        tells MongoDB to update the record if it exists, and create it if it doesn’t
     (4)the multi argument lets you specify whether all matching documents should be updated or just the first one (the default action).
        Note that the multi argument only works with $ operators.  
     (5)$inc   {$inc: {"Read" : 4} } 给read +4
     (6)$set   {$set : { Genre :"Sci-Fi" }
     (7)$unset  {$unset : { "Genre" : 1 } }
     (8)$push 如果是已经存在的数组字段,则加入,如果不存在,则创建字段数组,如果存在不是数组,则报错
     (9)$pushAll 规则同$push 一样
     (10)$addToSet
     (11)$pop,$pull, $pullAll
分享到:
评论

相关推荐

    mongodb 操作命令(全)

    以下是一些主要的MongoDB操作命令的详细解释: 1. **数据库相关操作**: - `use &lt;database&gt;`:切换当前工作数据库。例如,`use myDatabase` 将会切换到名为myDatabase的数据库。 - `db`:显示当前工作数据库。 -...

    MongoDB常用命令批处理

    在管理和操作MongoDB时,批处理脚本是一个高效的方法,特别是对于执行重复性的任务,如安装、启动、停止和配置服务。以下是根据提供的文件名解析出的MongoDB相关知识点: 1. **安装MongoDB服务**: - `install.bat...

    MongoDB数据库常用操作命令8.pdf

    总的来说,理解并熟练运用这些MongoDB操作命令对于高效地管理和处理数据至关重要。随着NoSQL数据库的普及,MongoDB因其强大的功能和易用性,已经成为很多企业和开发者的选择。通过不断学习和实践,你将能够更好地...

    MongoDB_命令.docx 命令集合

    它会显示一系列基本的数据库和集合操作命令。更具体的,你可以输入`db.help()`来查看当前数据库支持的方法,或者`db.&lt;collection&gt;.help()`获取特定集合的操作帮助。 6. **数据操作**:例如,`db.&lt;collection&gt;.find...

    nosql实验六- MongoDB的安装与基本操作.docx

    MongoDB 安装与基本操作 MongoDB 是一款流行的 NoSQL 数据库,广泛应用于大数据和实时 Web 应用程序。在本实验中,我们将学习如何在 Windows 和 Linux 环境下安装 MongoDB,并了解 MongoDB 的基本操作。 一、...

    mongodb基本命令和实例

    接下来是MongoDB的一些基本数据定义语言(DDL)和数据操作语言(DML)命令: 1. **创建数据库**: MongoDB中,无需显式创建数据库。当你首次使用 `use dbname` 时,如果数据库不存在,MongoDB会自动创建它。 2. *...

    mongodb 数据库基本操作.doc

    ### MongoDB数据库基本操作详解 #### 一、连接MongoDB数据库 MongoDB是一个广泛使用的开源文档数据库,支持多种编程语言。为了能够与MongoDB交互并执行各种数据库操作,首先需要通过官方提供的驱动程序或其他第三...

    mongodb常用命令大全

    - **示例**: 输入 `help` 或者 `db.help()` 可以查看基本操作命令的帮助。 ##### 2. 切换/创建数据库 (Use/Create Database) - **命令格式**: `use yourDB` - **功能描述**: 如果指定的数据库不存在,则会自动创建...

    MongoDB命令查询.txt

    在MongoDB中,各种操作主要通过命令来完成,这些命令可以分为几大类:查询命令、更新命令、聚合命令等。其中查询命令是使用频率最高的一类,它允许用户按照指定条件从集合中检索文档。 ### `findOne()` 命令详解 #...

    mongodb操作文档.doc

    MongoDB 的安装在Ubuntu系统上可以通过`sudo apt-get install -y mongodb`命令完成。启动、停止和重启MongoDB服务可以用`sudo service mongod start/stop/restart`。此外,可以通过`--dbpath`、`--logpath`等参数...

    MongoDB若基本操作

    - 启动MongoDB服务,通常通过执行特定的命令行指令,例如在Windows上运行`mongod.exe`。 2. **连接MongoDB** - 使用MongoDB的命令行客户端`mongo`来连接到服务器。 - 通过指定主机名和端口号(默认为`localhost:...

    MongoDB 权威指南.pdf

    #### 四、MongoDB操作命令 1. **基本操作**: - `use &lt;database&gt;`:切换到指定数据库。 - `db.&lt;collection&gt;.insert()`:向集合中插入文档。 - `db.&lt;collection&gt;.find()`:查询集合中的文档。 - `db....

    Mongodb操作三步骤

    在本文中,我们将深入探讨“MongoDB操作三步骤”,帮助您顺畅地完成MongoDB的环境搭建,以便于后续的数据存储和查询工作。 第一步:安装MongoDB 1. **下载MongoDB**:首先,访问MongoDB官方网站...

    Mongodb安装部署操作资料

    使用`use`命令切换数据库,`db.collection.find()`来查询数据,`db.collection.insertOne()`或`insertMany()`来插入数据,`db.collection.updateOne()`和`updateMany()`进行更新,`db.collection.deleteOne()`和`...

    MongoDB常用的操作命令.txt

    ### MongoDB常用操作命令详解 #### 一、启动与配置MongoDB服务 在开始介绍具体的数据库操作之前,我们先了解如何启动和配置MongoDB服务。以下是一些常用的命令: 1. **启动MongoDB服务(默认数据目录)** ```...

    mongodb创建用户操作命令

    mongodb创建用户操作命令

    MongoDB常用操作命令大全

    MongoDB常用操作命令大全 数据库常用命令 Collection聚集集合 用户相关 聚集集合查询

    详解MongoDB管理命令

    MongoDB 是一种流行的开源文档...在日常操作中,MongoDB 的管理命令是管理员进行数据管理、备份、恢复、性能优化以及问题排查的关键工具。熟练掌握这些命令,能够有效地管理和维护 MongoDB 集群,确保系统的稳定运行。

    mongodb数据库的基本操作

    在本文中,我们将深入探讨“mongodb数据库的基本操作”,包括增、删、改、查以及分页功能。 首先,让我们从创建数据库开始。在MongoDB中,数据库是存储数据的基本单位。通过运行`use &lt;database_name&gt;`命令,你可以...

    MongoDB常用命令汇总

    包含对数据库、集合、文档的常用操作。

Global site tag (gtag.js) - Google Analytics