专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Go

mongoDB根本使用(二)

发布时间:2011-06-29 18:14:52 文章来源:www.iduyao.cn 采编人员:星星草
mongoDB基本使用(二)

数据库基本操作

连接到mongoDB服务器 ./bin/mongo 127.0.0.1:12345 

查看当前数据库
> show dbs
admin  (empty)
local  0.078G

却换数据库(如果不存在会自动创建)
> use jerome
switched to db jerome

删除数据库
> 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

写入

(集合数据的写入,格式为JSON)
> db.jerome_collection.insert({x:1})
WriteResult({ "nInserted" : 1 })
(插入成功)

查询

> 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是全局字段,在数据库中不会重复)

测试:再插入一条数据_id为1
1
2
3
4
5
6
7
8
9
10
11
> db.jerome_collection.insert({x:3,_id:1})
WriteResult({ "nInserted" : 1 })
> db.jerome_collection.insert({x:2,_id:1})
WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 11000,
        "errmsg" "insertDocument :: caused by :: 11000 E11000 duplicate key error index: jerome.jerome_collection.$_id_  dup key: { : 1.0 }"
    }
})
插入两次一样的id会报错,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 }

更新

Updata至少接收两个参数,一个查找的,一个更新的数据。
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代表正向排序,-1代表反向排序
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})
{
    "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"
    }
]
(使用数据库之前创建索引更好)

索引虽然会使写入的数度变慢,但是查询的速度变快了。


友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

  • ModernUI课程:定义一个Logo

    ModernUI教程:定义一个Logo ModernWindow的标题栏包含了一块区域用来显示自定义的窗体Logo: 这个窗体logo通过ModernWindow.LogoD...

  • Django忘记管理员账号和密码的解决方法

    Django忘记管理员账号和密码的解决办法 看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了。结果搭建成功以后,一直...

  • GO语言小结(1)——基本知识

    GO语言总结(1)——基本知识 1、注释(与C++一样)   行注释://  块注释:/*   ...  */ 2、标识符   可以这么说,除了数字开头...

  • golang 惯用的文件读取方式

    golang 常用的文件读取方式 Golang 的文件读取方法很多,刚上手时不知道怎么选择,所以贴在此处便后速查。 一次性读取 小文件推荐一...

  • 查询深圳市通相关信息

    查询深圳通相关信息 用 HTTP.GET 从开放 API 中查询深圳通信息,然后将 JSON 数据存入结构体中,再格式化输出。 注意:获取的并不是实...

  • Go语言设计模式实践:结合(Composite)

    Go语言设计模式实践:组合(Composite) 关于本系列 这个系列首先是关于Go语言实践的。在项目中实际使用Go语言也有段时间了,一个体会就...

  • 列出索引和遍历目录

    列出目录和遍历目录 获取目录列表用 ioutil.ReadDir(),遍历目录用 filepath.Walk(),使用方法请参考文章示例。 示例代码: package ma...

  • io 包的惯用接口速记

    io 包的常用接口速记 我没有 C/C++ 基础,没有接口的概念,且从 Python 投奔而来,Python 的极简主义(一个结果往往只提供一个方法),让我在...

  • 代理服务扩充

    代理服务扩展 之前自己实现了一个代理服务,当时考虑的是只要支持SOCKS5就好了,因为我经常用CHROME,配合着SwitchySharp,体验还是很棒...

  • 文件的创造与打开

    文件的创建与打开 文件操作是个很重要的话题,使用也非常频繁,熟悉如何操作文件是必不可少的。Golang 对文件的支持是在 os package ...

热门推荐: