MongoDB 基本使用
2024-08-31 15:17:10
数据库基本操作
连接到mongoDBserver
1
|
. /bin/mongo 127.0.0.1:12345 |
查看当前数据库
1
2
3
|
> show dbs admin (empty) local 0.078G |
却换数据库(假设不存在会自己主动创建)
1
2
|
> use jerome switched to db jerome |
删除数据库
1
2
|
> db.dropDatabase() { "dropped" : "jerome" , "ok" : 1 } |
删除表
1
2
3
4
5
6
7
8
9
10
|
> > show tables jerome_collection jerome_coolection system.indexes > db.jerome_collection.drop() true > show tables #删除了当前表了 jerome_coolection system.indexes > |
写入
1
2
|
> db.jerome_collection.insert({x:1}) #集合数据的写入,格式为JSON WriteResult({ "nInserted" : 1 }) |
查询
1
2
3
4
5
6
7
8
9
10
11
|
> show dbs admin (empty) jerome 0.078GB local 0.078GB > show collections jerome_collection system.indexes > db.jerome_collection. find () { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 1 } > db.jerome_collection. find ({x:1}) #能够指定參数 { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 1 } #_id是全局字段,在数据库中不会反复 |
插入多条数据測试limit等
1
2
3
4
5
6
7
8
|
> for (i=3;i<100;i++)db.jerome_collection.insert({x:i}) #能够使用js语法 WriteResult({ "nInserted" : 1 }) > db.jerome_collection. find ().count() #查找总条数 99 > db.jerome_collection. find ().skip(3).limit(2). sort ({x:1}) #跳过前三条。取两条,使用x排序 { "_id" : ObjectId( "556ff5e8d7e60a53de941a74" ), "x" : 4 } { "_id" : ObjectId( "556ff5e8d7e60a53de941a75" ), "x" : 5 } > |
更新
1
2
3
4
5
6
7
8
|
> db.jerome_collection. find ({x:1}) { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 1 } > db.jerome_collection.update({x:1},{x:999}) #两个參数,一个查找的,一个更新的数据。 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({x:1}) #已经找不到了 > db.jerome_collection. find ({x:999}) { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 999 } > |
部分更新操作符(set )
1
2
3
4
5
6
7
|
> db.jerome_collection.insert({x:100,y:100,z:100}) WriteResult({ "nInserted" : 1 }) > db.jerome_collection.update({z:100},{$ set :{y:99}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({z:100}) { "_id" : ObjectId( "556ff84a1c99195ded71252e" ), "x" : 100, "y" : 99, "z" : 100 } > |
更新不存在数据时会自己主动创建
1
2
3
4
5
6
7
8
9
10
|
> db.jerome_collection. find ({y:100}) > db.jerome_collection.update({y:100},{y:999}, true ) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId( "556ff9556db7cf8009b5edf8" ) }) > db.jerome_collection. find ({y:999}) { "_id" : ObjectId( "556ff9556db7cf8009b5edf8" ), "y" : 999 } |
更新多条数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
> for (i=0;i<3;i++)db.jerome_collection.insert({c:2}) #插入三条 WriteResult({ "nInserted" : 1 }) > db.jerome_collection. find ({c:2}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 2 } > db.jerome_collection.update({c:2},{c:3}) #更新 WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({c:2}) { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 2 } > db.jerome_collection. find ({c:3}) #发现仅仅更新一条,是为了防止误操作 { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } > db.jerome_collection.update({c:2},{$ set :{c:3}}, false , true ) #更新多条 WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.jerome_collection. find ({c:2}) > db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } |
删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
> db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } > db.jerome_collection.remove() #不可用 2015-06-04T00:15:34.444-0700 remove needs a query at src /mongo/shell/collection .js:299 > db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } > db.jerome_collection.remove({c:3}) #删除必需要有參数 WriteResult({ "nRemoved" : 3 }) > db.jerome_collection. find ({c:3}) #删除成功 > |
索引
数据较多时。使用索引速度加快查询。
查看集合索引情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
> for (i=0;i<100;i++)db.jerome_collection.insert({x:i}) #加入測试数据 WriteResult({ "nInserted" : 1 }) > db.jerome_collection.getIndexes() #仅仅有一个默认索引 [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_" , "ns" : "jerome.jerome_collection" } ] > |
创建索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
> db.jerome_collection.ensureIndex({x:1}) #1代表正向排序,-1代表反向排序 { "createdCollectionAutomatically" : false , "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.jerome_collection.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_" , "ns" : "jerome.jerome_collection" }, { "v" : 1, "key" : { "x" : 1 }, "name" : "x_1" , "ns" : "jerome.jerome_collection" } ] > |
索引尽管会使写入的数度变慢。可是查询的速度变快了。
最新文章
- 安卓自定义组合控件--toolbar
- Android ORM -- Litepal(2)
- Poco::JSON::Array 中object 设置preserveInsertionOrder 时,stringify出错-->;深入解析
- jsp页面样例及解析
- 项目之solr全文搜索工具之创建项目索引库
- python的一些图像操作
- html 字体加粗
- 开通了cnblogs
- ASP.NET Core 行军记 -----拔营启程
- 随机变量的方差variance &; 随机向量的协方差矩阵covariance matrix
- WebDriver 运行模式下使用rc 代码
- 导出excel的简单方法
- hp-ux-ia64:jffi/ffi 编译总结
- windows下绑定线程(进程)到指定的CPU核心
- 数据结构和算法总结(一):广度优先搜索BFS和深度优先搜索DFS
- 查看linux是ubuntu还是centos
- 阶段02JavaWeb基础day04mysql
- JavaScript学习摘要
- InnoDB和MyISAM的区别
- Swift Assert 断言
热门文章
- OUTLOOK网站直接点击发送邮件
- C语言Huffman压缩和解压
- k8s日志收集配置
- 紫书 习题 8-15 UVa 1617 (贪心)
- codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
- C#-CLR各版本特点
- 洛谷 P2970 [USACO09DEC]自私的放牧Selfish Grazing
- scratchIDE使用说明
- [React] Use React Fragments to make your DOM tree cleaner
- mysql-过程与函数