MongoDB 基本使用
数据库基本操作
|
1
|
./bin/mongo 127.0.0.1:12345 |
|
1
2
3
|
> show dbsadmin (empty)local 0.078G |
|
1
2
|
> use jeromeswitched to db jerome |
|
1
2
|
> db.dropDatabase(){ "dropped" : "jerome", "ok" : 1 } |
|
1
2
3
4
5
6
7
8
9
10
|
> > show tablesjerome_collectionjerome_coolectionsystem.indexes> db.jerome_collection.drop()true> show tables #删除了当前表了jerome_coolectionsystem.indexes> |
写入
|
1
2
|
> db.jerome_collection.insert({x:1}) #集合数据的写入,格式为JSONWriteResult({ "nInserted" : 1 }) |
查询
|
1
2
3
4
5
6
7
8
9
10
11
|
> show dbsadmin (empty)jerome 0.078GBlocal 0.078GB> show collectionsjerome_collectionsystem.indexes> db.jerome_collection.find(){ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 }> db.jerome_collection.find({x:1}) #能够指定參数{ "_id" : ObjectId("556fd29a4e8b96c5ebc42e63"), "x" : 1 } #_id是全局字段,在数据库中不会反复 |
|
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 }> |
|
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" }]> |
MongoDB 基本使用的更多相关文章
- 【翻译】MongoDB指南/聚合——聚合管道
[原文地址]https://docs.mongodb.com/manual/ 聚合 聚合操作处理数据记录并返回计算后的结果.聚合操作将多个文档分组,并能对已分组的数据执行一系列操作而返回单一结果.Mo ...
- 【翻译】MongoDB指南/CRUD操作(四)
[原文地址]https://docs.mongodb.com/manual/ CRUD操作(四) 1 查询方案(Query Plans) MongoDB 查询优化程序处理查询并且针对给定可利用的索引选 ...
- 【翻译】MongoDB指南/CRUD操作(三)
[原文地址]https://docs.mongodb.com/manual/ CRUD操作(三) 主要内容: 原子性和事务(Atomicity and Transactions),读隔离.一致性和新近 ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- 【翻译】MongoDB指南/CRUD操作(一)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删 ...
- CRL快速开发框架系列教程十二(MongoDB支持)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- MongoDB系列(二):C#应用
前言 上一篇文章<MongoDB系列(一):简介及安装>已经介绍了MongoDB以及其在window环境下的安装,这篇文章主要讲讲如何用C#来与MongoDB进行通讯.再次强调一下,我使用 ...
- MongoDB系列(一):简介及安装
什么是MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为应用提供可扩展的高 ...
- [原]分享一下我和MongoDB与Redis那些事
缘起:来自于我在近期一个项目上遇到的问题,在Segmentfault上发表了提问 知识背景: 对不是很熟悉MongoDB和Redis的同学做一下介绍. 1.MongoDB数组查询:MongoDB自带L ...
- 用MongoDB分析合肥餐饮业
看了<从数据角度解析福州美食>后难免心痒,动了要分析合肥餐饮业的念头,因此特地写了Node.js爬虫爬取了合肥的大众点评数据.分析数据库我并没有采用MySQL而是用的MongoDB,是因为 ...
随机推荐
- 《鸟哥的Linux私房菜》读书笔记--第0章 计算机概论 硬件部分
一个下午看了不少硬件层面的知识,看得太多太快容易忘记.于是在博客上写下读书笔记. 有关硬件 个人计算机架构&接口设备 主板芯片组为“南北桥”的统称,南北桥用于控制所有组件之间的通信. 北桥连接 ...
- Gitlab command line instructions
Git global setup git config --global user.name "winner" git config --global user.email &qu ...
- Redis介绍以及安装具体解释
redis是一个key-value存储系统. 和Memcached类似.它支持存储的value类型相对很多其它,包含string(字符串).list(链表).set(集合).zset(sorted s ...
- android将String转化为MD5的方法+一些String经常使用的方法
public class StringUtils { public static String MD5Encode(String origin) { String resultString = nul ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- C++ 学习笔记(一些新特性总结3)
C++ 学习笔记(一些新特性总结3) public.protected 和 private 继承 public 继承时,基类的存取限制是不变的. class MyClass { public: // ...
- 为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
为什么button在设置标题时要用一个方法.而不像lable一样直接用一个属性 原因是有时我们对 button做一次点击,须要改变button的标题.仅仅实用方法才干做到,而label是标签 ...
- 转:百分百激活office for mac2011的激活文件
方法:1点击finder 2点击系统盘 3点击资源库 4找到Preferences文件夹 5用压缩包里的 ...
- 12、NIO、AIO、BIO二
一.NIO2快速读写文件 写完之后记得flush一下,NIO2不能自行创建文件,需要在文件中判断一下. package com.zxc.L; import org.junit.Test; import ...
- python Flask 学前班
0.Flask简单介绍 Flask是一个用Python编写的轻量级的Web应用框架.本文第一部分将简单解说Flask的安装,接着展示一个Flask的样例,第一个样例非常easy但也存在缺陷-- ...