Mongodb学习笔记二(Mongodb基本命令)
第二章 基本命令
一、Mongodb命令
说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法。
对于database和collection无需主动创建,在插入数据时,如果database和collection不存在则会自动创建。
常用命令
help命令
通过此命令可以看到一些最基本的命令,如图:

use命令
例如命令【use demodb】,创建demodb,不用担心demodb不会创建,当使用use demodb 命令创建第一个collection时会自动创建数据库demodb,如图: 
插入数据
使用命令【db.collectionName.insert({name:"jack",age:33})】collectionName中插入一个document,如果collectionName不存在则创建。 使用命令【db.getCollectionNames()】会得到collectionName和system.indexex。system.indexex对于每个database都有,用于记录index。 使用命令【db.collectionName.find()】会查看到collectionName中的所有document。 命令如下:
E:\MongoDB\bin>mongo
MongoDB shell version: 2.6.
connecting to: test
> use demodb
switched to db demodb
> db.FirstCollection.insert({name:"jack",age:22})
WriteResult({ "nInserted" : 1 })
> show collections
FirstCollection
system.indexes
> db.getCollectionNames()
[ "FirstCollection", "system.indexes" ]
> db.demodb.find()
> db.FirstCollection.find()
{ "_id" : ObjectId("543731431dc491f307663a0d"), "name" : "jack", "age" : 22 }
> db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "demodb.FirstCollection" }
查询数据
参考网址:http://www.cnblogs.com/stephen-liu74/archive/2012/08/03/2553803.html
MongoDB使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.
find的第一个参数决定了要返回哪些文档(document的过滤条件).其形式也是一个文档,说明要查询的细节.空的查询文档{}会匹配集合的全部内容.
要是不指定查询文档,默认是{}.如:db.users.find()返回集合中的所有内容.
向查询文档中添加键值对,就意味着添加了查询条件.对绝大多数类型来说,整数匹配整 数,布尔类型匹配布尔类型,字符串匹配字符串.
先添加测试数据
db.Student.insert({name:"jack",sex:1,age:33})
db.Student.insert({name:"jack",sex:1,age:33})
db.Student.insert({name:"lily",sex:0,age:13})
db.Student.insert({name:"kaily",sex:0,age:33})
db.Student.insert({name:"tom",sex:1,age:53})
1、find()/findOne()条件过滤
只获取name等于jack的Student。findOne()则只获取第一条
> use demodb
switched to db demodb
> db.Student.find({name:"jack"})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age
" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age
" : 33 }
> db.Student.findOne({name:"jack"})
{
"_id" : ObjectId("5437383157abafe09d99cbfc"),
"name" : "jack",
"sex" : 1,
"age" : 33
}
2、find()/findOne()指定返回的fileds
说明:find()的第二个参数限制返回的filed的个数,0代表不返回,1代表返回。"_id"键总是会被返回。
如果不带条件,只限制返回的filed个数的话,命令如下:db.Student.find({},{sex:0})。只需要第一个参数为{}空字典就可以。
只获取name等于jack的Student,并且filed为name,age的数据。
> db.Student.find({name:"jack"},{name:1,age:1})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "age" : 33 }
> db.Student.find({name:"jack"},{sex:0})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "age" : 33 }
>
3、查询条件
"$lt","$lte","$gt","$gte"分别对应<,<=,>,>=
如下代码:
db.Student.find({age:{$gt:33}}) 查询age大于33的
db.Student.find({age:{$gte:33}})
db.Student.find({age:{$lt:33}})
db.Student.find({age:{$lte:33}})
db.Student.find({age:{$gt:23,$lt:43}})
> db.Student.find({age:{$gt:33}})
{ "_id" : ObjectId("543738c357abafe09d99cc00"), "name" : "tom", "sex" : 1, "age" : 53 }
> db.Student.find({age:{$gte:33}})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
{ "_id" : ObjectId("543738c357abafe09d99cc00"), "name" : "tom", "sex" : 1, "age" : 53 }
> db.Student.find({age:{$lt:33}})
{ "_id" : ObjectId("543738b857abafe09d99cbfe"), "name" : "lily", "sex" : 0, "age" : 13 }
> db.Student.find({age:{$lte:33}})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfe"), "name" : "lily", "sex" : 0, "age" : 13 }
{ "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
> db.Student.find({age:{$gt:23,$lt:43}})
{ "_id" : ObjectId("5437383157abafe09d99cbfc"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbfd"), "name" : "jack", "sex" : 1, "age" : 33 }
{ "_id" : ObjectId("543738b857abafe09d99cbff"), "name" : "kaily", "sex" : 0, "age" : 33 }
$ne 代表不等于
db.Student.find({age:{$ne:33}}) 查询age不等于33
$in,$not和$or
db.Student.find({age:{$in:[13,53]}})
db.Student.find(
{
$or:
[
{age:{$in:[13,53]}},
{name:"kaily"}
]
}
)
4、特殊查询--null和exists
null可以匹配自身,而且可以匹配"不存在的"
--插入测试数据
db.Student.insert({name:null,sex:1,age:18})
db.Student.insert({sex:1,age:24})
db.Student.find({name:null}) --上面两条都能查到
db.Student.find({name:{$in:[null],$exists:true}}) ---只能查到第一条
5、数组数据查询
db.Student.insert({name:"wjh",sex:1,age:18,color:["red","blue","black"]})
db.Student.insert({name:"lpj",sex:1,age:22,color:["white","blue","black"]})
db.Student.find()
--color数组中所有包含white的文档都会被检索出来
db.Student.find({color:"white"})
--color数组中所有包含red和blue的文档都会被检索出来,数组中必须同时包含red和blue,但是他们的顺序无关紧要。
db.Student.find({color:{$all:["red","blue"]}})
--精确匹配,即被检索出来的文档,color值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致。
db.Student.find({color:["red","blue","black"]})
--匹配数组中指定下标元素的值。数组的起始下标是0。注意color要加引号。
db.Student.find({"color.0":"white"})
6、内嵌文档查询
----待完成-------
7、排序
db.Student.find().sort({age:1})
db.Student.find().sort({age:1,sex:1})
--1代表升序,-1代表降序
8、分页
db.Student.find().sort({age:1}).limit(3).skip(3)
--limit代表取多少个document,skip代表跳过前多少个document。
9、获取数量
db.Student.count({name:null})
--或者
db.Student.find({name:null}).count()
删除数据
说明:删除数据比较简单。
db.Student.remove({name:null})
更新数据
1.更新数据
命令【db.Student.update({name:"jack"},{age:55})】执行后
先查询name=jack的所有document,然后将name=jack的所有document都替换为{age:55},其它filed都没有了。
正确的更新方式应该为:
db.Student.update({name:"jack"},{$set:{age:55}})
2.增加field
--将name=lily的student增加一个filed height
db.Student.update({name:"lily"},{$inc:{height:175}})
3.upset-将数字field增加多少增量
--若存在则添加,否则更新,只能用于数字field,例如age更新前是50,更新了185,则变为235.
db.Student.update({name:"lily"},{$inc:{age:185}},true)
4.批量更新数据
------------------待完成---------
Mongodb学习笔记二(Mongodb基本命令)的更多相关文章
- MongoDB学习笔记二- Mongoose
MongoDB学习笔记二 Mongoose Mongoose 简介 之前我们都是通过shell来完成对数据库的各种操作, 在开发中大部分时候我们都需要通过程序来完成对数据库的操作 而Mongoose就 ...
- Mongodb学习笔记一(Mongodb环境配置)
Mongodb学习 说明: MongoDB由databases组成,database由collections组成,collection由documents组成,document由fileds组成.Mo ...
- MongoDB学习笔记二—Shell操作
数据类型 MongoDB在保留JSON基本键/值对特性的基础上,添加了其他一些数据类型. null null用于表示空值或者不存在的字段:{“x”:null} 布尔型 布尔类型有两个值true和fal ...
- MongoDB学习笔记一(MongoDB介绍 + 基本指令 + 查询语句)
什么是MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供可扩 ...
- MongoDB学习笔记(一) MongoDB介绍及安装(摘)
MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式.Mongo使用C++开发.Mongo的官方网 ...
- MongoDb 学习笔记(一) --- MongoDb 数据库介绍、安装、使用
1.数据库和文件的主要区别 . 数据库有数据库表.行和列的概念,让我们存储操作数据更方便 . 数据库提供了非常方便的接口,可以让 nodejs.php java .net 很方便的实现增加修改删除功能 ...
- MongoDB学习笔记(二) 通过samus驱动实现基本数据操作
传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由(database).集合(collection).文档对象(documen ...
- MongoDB学习笔记二:使用Docker安装MongoDB
目录 Docker安装MongoDB Docker给MongoDB设置用户密码 NoSQL Manager for MongoDB连接 为admin赋权限 上一个笔记介绍了Windows下安装Mong ...
- MongoDB学习笔记二:创建、更新及删除文档
插入并保存文档 对目标集使用insert方法插入一个文档: > db.foo.insert({"bar" : "baz"}) 这个操作会给文档增加一个&q ...
随机推荐
- 微信WeixinJSBridge API(屏蔽右上角按钮等)
[声明]:我这份纯属于备份,为了自己将来用起来方便: [相关链接]:http://www.2cto.com/weixin/201511/451592.html(好不好用完全看这个文档的作者了) [感谢 ...
- 第一次react-native项目实践要点总结
今天完成了我的第一个react-native项目的封包,当然其间各种环境各种坑,同时,成就感也是满满的.这里总结一下使用react-native的一些入门级重要点(不涉及环境).注意:阅读需要语法基础 ...
- centos 6.0用yum安装中文输入法
Centos6.2代码 CentOS 6.0没有默认没有装语言支持(Language Support),因此很不方面. 终于发现了有效的方法: su root yum install "@C ...
- entity Framework codefirst Migrations
一次数据迁移的记录 首先在vs工具里面使用打开程序包管理器控制台 在控制台上面选择程序集为数据访问层 注意配置生成app里面的连接字符串 在控制台输入 Enable-Migrations 会自动生成一 ...
- React Native知识7-TabBarIOS组件
一:简介 两个TabBarIOS和TabBarIOS.Item组件可以实现页面Tab切换的功能,Tab页面切换的架构在应用开发中还是非常常见的.如:腾讯QQ,淘宝,美团外卖等等 二:TabBarIOS ...
- SQLite浅析
对于iOS工程师有一道常考的面试题,即iOS数据存储的方式 标答如下: Plist(NSArray\NSDictionary) Preference (偏好设置\NSUserDefaults) NSC ...
- CSS3 text-shadow
<!DOCTYPE html > <html > <head> <meta charset="utf-8"> <title&g ...
- Oracle RAMN 备份解决方案一例
以前在博客里面介绍了RMAN备份脚本一列分享,通过RMAN备份到本地路径,然后通过FTP将备份文件上传到FTP服务器. 下面简单介绍另外一例RMAN备份解决方案,下面是我简单画的一个图(很少画图,感觉 ...
- SQL SERVER 属性OWNER不可用于数据库xxx。该对象可能没有此属性,也可能是访问权限不足而无法检索。
今天遇到一个案例:右键单击数据库的属性时出现下面错误提示: 属性Owner不可用于数据库xxx,该对象可能没有此属性,也可能是访问权限不足而无法检索. 使用脚本查看该数据库的Owner时发现Owner ...
- SQL Server Column Store Indeses
SQL Server Column Store Indeses SQL Server Column Store Indeses 1. 概述 2. 索引存储 2.1 列式索引存储 2.2 数据编码和压缩 ...