MongoDB学习教程(3)-常用命令

1.MongoDB 条件操作符

描述

条件操作符用于比较两个表达式并从mongoDB集合中获取数据。

在本章节中,我们将讨论如何在MongoDB中使用条件操作符。

MongoDB中条件操作符有:

    • (>) 大于 - $gt
    • (<) 小于 - $lt
    • (>=) 大于等于 - $gte
    • (<= ) 小于等于 - $lte

MongoDB (>) 大于操作符 - $gt

>
> db.col1.find()
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "name" : "300567", "price" : 57 }
{ "_id" : ObjectId("59aaa48ab211b47e25634652"), "name" : "300567", "price" : 59 }
{ "_id" : ObjectId("59aaa4b6b211b47e25634653"), "name" : "300000", "price" : 59 }
> db.col1.find({'price':{$gt:58}})
{ "_id" : ObjectId("59aaa48ab211b47e25634652"), "name" : "300567", "price" : 59 }
{ "_id" : ObjectId("59aaa4b6b211b47e25634653"), "name" : "300000", "price" : 59 }
>
View Code

类似于SQL语句:

Select * from col1 where price > 58;

MongoDB (<) 小于操作符 - $lt

> db.col1.find({'price':{$lt:58}})
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "name" : "300567", "price" : 57 }
>
View Code

类似于SQL语句:

Select * from col1 where price < 58;

MongoDB(>=)大于等于操作符 - $gte

> db.col1.find({'price':{$gte:58}})
{ "_id" : ObjectId("59aaa48ab211b47e25634652"), "name" : "300567", "price" : 59 }
{ "_id" : ObjectId("59aaa4b6b211b47e25634653"), "name" : "300000", "price" : 59 }
>
View Code

类似于SQL语句:

Select * from col1 where price >= 58;

MongoDB (<=) 小于操作符 - $lte

> db.col1.find({'price':{$lte:58}})
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "name" : "300567", "price" : 57 }
>
View Code

类似于SQL语句:

Select * from col1 where price <= 58;

MongoDB 使用 (<) 和 (>) 查询 - $lt 和 $gt

> db.col1.find({'price':{$lt:58,$gt:56}})
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "name" : "300567", "price" : 57 }
>
View Code
类似于SQL语句:
Select * from col1 where price>56 AND price<58;

MongoDB $type 操作符

在本章节中,我们将继续讨论MongoDB中条件操作符 $type。

$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果。

MongoDB 中可以使用的类型如下表所示:

类型    数字    备注
Double    1     
String    2     
Object    3     
Array    4     
Binary data    5     
Undefined    6    已废弃。
Object id    7     
Boolean    8     
Date    9     
Null    10     
Regular Expression    11     
JavaScript    13     
Symbol    14     
JavaScript (with scope)    15     
32-bit integer    16     
Timestamp    17     
64-bit integer    18     
Min key    255    Query with -1.
Max key    127     
View Code
> db.col1.find()
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "name" : "300567", "price" : 57 }
{ "_id" : ObjectId("59aaa48ab211b47e25634652"), "name" : "300567", "price" : 59 }
{ "_id" : ObjectId("59aaa4b6b211b47e25634653"), "name" : "300000", "price" : 59 }
> db.col1.find({'price':{$type:2}})
> db.col1.find({'price':{$type:1}})
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "name" : "300567", "price" : 57 }
{ "_id" : ObjectId("59aaa48ab211b47e25634652"), "name" : "300567", "price" : 59 }
{ "_id" : ObjectId("59aaa4b6b211b47e25634653"), "name" : "300000", "price" : 59 }
>
View Code

2.MongoDB Limit与Skip方法


MongoDB Limit() 方法

如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。

语法

limit()方法基本语法如下所示:

>db.COLLECTION_NAME.find().limit(NUMBER)
> db.col1.find({'price':{$type:1}},{_id:0}).limit(1)
{ "name" : "300567", "price" : 57 }
> db.col1.find({'price':{$type:1}},{_id:1}).limit(1)
{ "_id" : ObjectId("59aaa2cab211b47e25634651") }
> db.col1.find({'price':{$type:1}},{_id:1,'price':1}).limit(1)
{ "_id" : ObjectId("59aaa2cab211b47e25634651"), "price" : 57 }
> db.col1.find({'price':{$type:1}},{_id:0,'price':1}).limit(1)
{ "price" : 57 }
> db.col1.find({'price':{$type:1}},{_id:0}).limit(1)
{ "name" : "300567", "price" : 57 }
> db.col1.find({'price':{$type:1}},{_id:0}).limit(1)
{ "name" : "300567", "price" : 57 }
>
View Code

注:如果你们没有指定limit()方法中的参数则显示集合中的所有数据。


MongoDB Skip() 方法

我们除了可以使用limit()方法来读取指定数量的数据外,还可以使用skip()方法来跳过指定数量的数据,skip方法同样接受一个数字参数作为跳过的记录条数。

语法

skip() 方法脚本语法格式如下:

>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

> db.col1.find({'price':{$type:1}},{_id:0})
{ "name" : "300567", "price" : 57 }
{ "name" : "300567", "price" : 59 }
{ "name" : "300000", "price" : 59 }
> db.col1.find({'price':{$type:1}},{_id:0}).skip(1)
{ "name" : "300567", "price" : 59 }
{ "name" : "300000", "price" : 59 }
> db.col1.find({'price':{$type:1}},{_id:0}).skip(2)
{ "name" : "300000", "price" : 59 }
View Code

注:skip()方法默认参数为 0 。

MongoDB 排序


MongoDB sort()方法

在MongoDB中使用使用sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列。

语法

sort()方法基本语法如下所示:

>db.COLLECTION_NAME.find().sort({KEY:1})

> db.col1.find({'price':{$type:1}},{_id:0}).sort({'price':1})
{ "name" : "300567", "price" : 57 }
{ "name" : "300567", "price" : 59 }
{ "name" : "300000", "price" : 59 }
> db.col1.find({'price':{$type:1}},{_id:0}).sort({'price':-1})
{ "name" : "300567", "price" : 59 }
{ "name" : "300000", "price" : 59 }
{ "name" : "300567", "price" : 57 }
>
View Code

MongoDB 索引

索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录。

这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对网站的性能是非常致命的。

索引是特殊的数据结构,索引存储在一个易于遍历读取的数据集合中,索引是对数据库表中一列或多列的值进行排序的一种结构


ensureIndex() 方法

MongoDB使用 ensureIndex() 方法来创建索引。

语法

ensureIndex()方法基本语法格式如下所示:

>db.COLLECTION_NAME.ensureIndex({KEY:1})

语法中 Key 值为你要创建的索引字段,1为指定按升序创建索引,如果你想按降序来创建索引指定为-1即可。

> db.col1.ensureIndex({'title':1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.col1.ensureIndex({'title':1,'price':-1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}
> db.col1.ensureIndex({'title':1,'price':-1},{background:true})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 3,
        "numIndexesAfter" : 3,
        "note" : "all indexes already exist",
        "ok" : 1
}
View Code
ensureIndex() 接收可选参数,可选参数列表如下:
Parameter    Type    Description
background    Boolean    建索引过程会阻塞其它数据库操作,background可指定以后台方式创建索引,即增加 "background" 可选参数。 "background" 默认值为false。
unique    Boolean    建立的索引是否唯一。指定为true创建唯一索引。默认值为false.
name    string    索引的名称。如果未指定,MongoDB的通过连接索引的字段名和排序顺序生成一个索引名称。
dropDups    Boolean    在建立唯一索引时是否删除重复记录,指定 true 创建唯一索引。默认值为 false.
sparse    Boolean    对文档中不存在的字段数据不启用索引;这个参数需要特别注意,如果设置为true的话,在索引字段中不会查询出不包含对应字段的文档.。默认值为 false.
expireAfterSeconds    integer    指定一个以秒为单位的数值,完成 TTL设定,设定集合的生存时间。
v    index version    索引的版本号。默认的索引版本取决于mongod创建索引时运行的版本。
weights    document    索引权重值,数值在 199,999 之间,表示该索引相对于其他索引字段的得分权重。
default_language    string    对于文本索引,该参数决定了停用词及词干和词器的规则的列表。 默认为英语
language_override    string    对于文本索引,该参数指定了包含在文档中的字段名,语言覆盖默认的language,默认值为 language.
View Code

实例

在后台创建索引:

db.values.ensureIndex({open: 1, close: 1}, {background: true})

通过在创建索引时加background:true 的选项,让创建工作在后台执行

原文地址:https://www.cnblogs.com/william126/p/7468028.html