MongoDB学习笔记

来源:互联网 发布:mysql破解教程 编辑:程序博客网 时间:2024/05/22 13:09
一、安装

下载地址:http://www.mongodb.org/downloads
按步骤进行安装,类型我选的是custom

二、配置环境

先将mongodb的安装目录下的bin文件配置到环境变量中,这样不用每次都cd到安装目录下
比如我的bin文件路径是:D:\program files\mongodb\bin
将这个路径配置到高级环境变量的系统变量的Path中。

先将数据库的数据存储地方进行配置,执行以下语句

mongod.exe --dbpath c:\data\db

显示:



出现waiting for connection on port 27017说明成功。
这个命令框不能关,要一直开着。然后打开另一个命令框
运行:mongo

显示如下:



这说明MongoDB已经安装成功啦!!!

三、基本概念

  SQL术语  MongoDB术语  说明 database database  数据库 table collection 数据库表/集合 row document 数据记录行/文档 column field 数据字段/域 index index 索引 table joins  表连接,MongoDB不支持 primary key primary key 主键,MongoDB自动将_id字段设置为主键


四、基本操作

(1)
show dbs 显示所有数据的列表

> show dbslocal  0.078GBtest   0.078GB

(2)
db  显示当前数据库对象或集合

> dbtest

(3)
use db1  连接一个指定的数据库,若这个数据库不存在则会新建一个db1数据库

> use localswitched to db local> dblocal> 

(4)查看数据库中所有的集合
show collections
或 show collections in current database

(5)
db.dropDatabase() 删除当前数据库
db.copyDatabase() 复制数据库
db.createCollection(name, {size:..., capped:..., max:..})  创建表
db.addUser(username, password)  添加数据库授权用户
db.removeUser(username)  删除数据库用户

(6)查询
db.collection.find(query, projection) 查询集合中文档并返回结果为游标的文档集合

// 返回集合中所有文档
db.collection.find()
//或者
db.collection.find({})

//进行条件筛选,返回user集合中age键值为16的文档集合。
db.user.find({age:16})

//查询年龄为28且性别为男性的文档集合
db.user.find({age:28, sex:"male"})

通过第二个参数projection来指定返回的键,如果不指定则默认返回查询文档中所有键值
例如user包含 _id, name, age, sex, email等键,查询结果只想显示集合中的 name 和 age键,可以使用如下查询返回这些键
db.user.find({}, {"name":1, "age":1}) 

上述语句执行后查询结果中 _id这个键总是被返回 ,即使没有指定也一样。但是我们可以显性的将其从查询结果中移除掉,语句如下:
db.user.find({}, {"name":1, "age":1, "_id":0})

# 第二个参数中,指定键名且值为1或者true说明要在查询结果中显示的键,若值为0或false,则为不显示键。

(7)一个增删改查的例子

代码:

# coding=utf-8import pymongoclient = pymongo.MongoClient("localhost", 27017)db = client.pythonprint db.nameprint db.my_collectiondb.my_collection.save({"x": 10})db.my_collection.save({"x": 8})db.my_collection.save({"x": 11})db.my_collection.save({"x": 13})db.my_collection.save({"x": 14, "y": 3})print db.my_collection.find_one()print "全部数据, x值"for item in db.my_collection.find():    print item["x"]db.my_collection.remove({"y": 3})print '删除后,全部数据, x值'for item in db.my_collection.find():    print item["x"]db.my_collection.update({"x": 8}, {"x": 2})print "更新后,全部数据, x值"for item in db.my_collection.find():    print item["x"]print db.my_collection.create_index("x")# 创建索引查询for item in db.my_collection.find().sort("x", pymongo.ASCENDING):    print item["x"]print [item["x"] for item in db.my_collection.find().limit(2).skip(1)]



输出:

pythonCollection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), u'python'), u'my_collection'){u'x': 10, u'_id': ObjectId('574bfaf6d5a7fe331c3b37af')}全部数据, x值108111314删除后,全部数据, x值1081113更新后,全部数据, x值1021113x_12101113[2, 11]



(8)数据导出
因为已经将mongodb安装目录下的bin文件添加到环境变量中了,因此现在的步骤都是直接可以在命令框中进行,而不需要cd到bin目录下了。

导出方式一:
-d表示从哪个数据库导出数据; -c 表示从哪个集合中导出数据; -o表示导出的数据输出到哪个文件中
mongoexport -d localdb -c user -o user.dat

导出方式二:
-f后面接的是想导出的数据键值
mongoexport -d localdb -c -user -f name, content --csv -o result.csv

注意文件的输出位置是在你的命令框当前所处的目录下。
0 0