如今NoSql这么火热,假期闲来无事也来凑凑热闹,多学点儿是点儿,好处肯定有,坏处肯定没。。。。
选了大家谈论最多的mongodb来学习,先去www.mongodb.org
下载,安装就直接解压到你就好了。
假设windows 32bit机,建立目录D:\mongodb 把压缩包bin目录下的exe全部放到mongodb目录下,然后建立一个data文件夹(D:\mongodb\data) 这个data目录就是用来存放数据的,默认是C盘data\db目录。
bin目录下的
mongod.exe
- 数据库服务器.
mongo.exe
- 管理员shell
当我们使用自定义的数据存储文件目录D:\mongodb\data时,使用cmd 进入D:\mongdodb目录
输入mongod --dbpath
D:\mongodb\data用来启动时指定数据存储目录。
然后运行mongo.exe就行了。
使用help命令就可以查看所有想要得到的命令。
下面进入开发学习阶段:
其实我真的很想就复制粘贴官网的文档,因为真的很容易懂,都懒得翻译写出来了,唉。。。。。
启动之后,将看到
By default the shell connects to database "test" on localhost. You then see:
MongoDB shell version: <whatever>
url: test
connecting to: test
type "help" for help
>
"connecting to:
" 显示了正在连接和使用的数据库。想要使用其它库, 键入:
> use mydb
switched to db mydb
用use命令切换数据库并不能立即创建数据库,数据库将在第一次插入数据时延迟创建。可以使用show dbs查看数据库。
Inserting Data into A Collection
下面的shell命令,创建了两个对象j和t,并把它们存入sha0k集合中,K-V,就像map一样,称它集合,不为过吧。
> j = { name : "mongo" };
{"name" : "mongo"}
> t = { x : 3 };
{ "x" : 3 }
> db.sha0k.save(j);
> db.sha0k.save(t);
> db.sha0k.find();
{ "_id" : ObjectId("4f2167ee......"), "name" : "mongo" }
{ "_id" : ObjectId("4f2168f....."), "x" : 3 }
>
对象Id太长了,我没写完,然后再使用show dbs就可以看到mydb已经在数据库列表中了,库创建完成。
有几点是需要注意的(我是真的懒得翻译了,见谅):
- We did not predefine the collection. The database creates it automatically on the first insert.(没有预先定义集合)
- The documents we store can have different fields - in fact in this
example, the documents have no common data elements at all. In
practice, one usually stores documents of the same structure within
collections.()
- Upon being inserted into the database, objects are assigned an object ID
(if they do not already have one) in the field _id
.
- When you run the above example, your ObjectID values will be different.
一次添加多个对象:
> for (var i = 1; i <= 20; i++) db.sha0k.save({x : 4, j : i});
> db.sha0k.find();
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
has more
我一共插入了20个数据,这里只显示了18个,并在下面提示has more,这是因为shell限制一个迭代的游标只到20,所以要查看下面的,需要使用it命令,iterator。。。。
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
has more
> it
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
Accessing Data From a Query
有没有发现刚的办法很麻烦,20多个我就得打次it,40多个打两次,依次类推。。。。
有没有简单的办法呢,那就来看下面的,刚刚提到,mongodb 找到的数据指向都是游标。
> var cursor = db.sha0k.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
第二行的printjson()函数,可以从环境中理解出来,就是将对象以json数据格式打印出来,上面的例子是游标风格的迭代。
> var cursor = db.sha0k.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
When using a cursor this way, note that all values up to the highest
accessed (cursor[4] above) are loaded into RAM at the same time. This
is inappropriate for large result sets, as you will run out of memory.
Cursors should be used as an iterator with any query which returns a
large number of elements.(请注意内存溢出)
数组访问方式:
> var arr = db.sha0k.find().toArray();
> arr[5];
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
Specifying What the Query Returns
SELECT * FROM things WHERE x=4
> db.sha0k.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "x" : 4, "j" : 20 }
A query document of the form { a:A, b:B, ... } means "where a==A and b==B and ...".
使用true关键字,只查找j的值结果集
SELECT j FROM things WHERE x=4
> db.sha0k.find({x:4}, {j:true}).forEach(printjson);
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "j" : 1 }
{ "_id" : ObjectId("4c220a42f3924d31102bd857"), "j" : 2 }
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "j" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "j" : 4 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85a"), "j" : 5 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85b"), "j" : 6 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85c"), "j" : 7 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85d"), "j" : 8 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85e"), "j" : 9 }
{ "_id" : ObjectId("4c220a42f3924d31102bd85f"), "j" : 10 }
{ "_id" : ObjectId("4c220a42f3924d31102bd860"), "j" : 11 }
{ "_id" : ObjectId("4c220a42f3924d31102bd861"), "j" : 12 }
{ "_id" : ObjectId("4c220a42f3924d31102bd862"), "j" : 13 }
{ "_id" : ObjectId("4c220a42f3924d31102bd863"), "j" : 14 }
{ "_id" : ObjectId("4c220a42f3924d31102bd864"), "j" : 15 }
{ "_id" : ObjectId("4c220a42f3924d31102bd865"), "j" : 16 }
{ "_id" : ObjectId("4c220a42f3924d31102bd866"), "j" : 17 }
{ "_id" : ObjectId("4c220a42f3924d31102bd867"), "j" : 18 }
{ "_id" : ObjectId("4c220a42f3924d31102bd868"), "j" : 19 }
{ "_id" : ObjectId("4c220a42f3924d31102bd869"), "j" : 20 }
休息了~~~~明天再学。。。。
分享到:
相关推荐
03★MongoDB学习★基本概念的理解
MongoDB的安装和基本使用命令,在window上安装MongoDB数据库
在MongoDB中,数据是以文档的形式存储的,文档是一个键值对的集合,并且文档支持嵌套和数组。MongoDB中的一个文档相当于关系型数据库中的一行,多个文档构成了集合(collection),集合类似于关系型数据库中的表。 ...
MongoDB基本查询.pdf 学习资料 复习资料 教学资源
主要给大家介绍了关于mongodb BSON的基本使用教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
mongodb快速学习大纲记录 1、启动 2、基本操作 3、细说增减删 4、高级操作 5、索引操作 6、主从复制 7、分片技术 8、运维技术 9、驱动
内容概括:提供了一份MongoDB期末考试测试题的文档,用于帮助同学复习MongoDB相关知识和技能。 适用人群:适用于需要复习MongoDB知识的同学,尤其是准备参加期末考试的学生。 使用场景及目标:该文档可以用作学生...
MongoDB学习手册 说明 一、 Mongodb简介 二、 MongoDB特性 适用场景: 不适用场景: 三、 MongoDB的工作方式 四、 MongoDB的下载 五、 MongoDB的安装 六、 MongoDB数据类型 1. Timestamp类型 2. ObjectId...
这是本人自己在学习过程中作的一些笔记,有利于初学者迅速掌握MongoDB的基本概念和操作。
总结的MongoDB的学习笔记,基本上包括了MongoDB的方方面面,每个知识点,都有示例代码,有需要的朋友,可以下载下来进一步补充完善。
mongodb分片配置.docx Mongodb.chm mongodb安装以及基本操作.docx MongoDB使用手册.doc mongo分布式研究.docx 有这5个文档足够学习Mongodb使用了
MongoDB 最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现...基本命令,增删改查,高级查询,Java连接MongoDB,MongoDB连接池,综合案例-网站点击日志分析,日志写入与查询
MongoDB,作为当今流行的NoSQL数据库之一,以其灵活的数据模型和强大的性能,在数据存储和处理领域占据了一席之地。对于开发者而言,掌握MongoDB的基本操作至关重要,它不仅能提升数据处理效率,还能为应用程序带来...
系统的学习MongoDB从入门到进阶,掌握现在火爆的NoSQL技术之一。 选择MongoDB的原因及其优势 MongoDB单机部署、副本集部署、分片部署以及相关操作 MongoDB的客户端连接和常用命令操作 SpringDataMongoDB对MongoDB的...
附件是mongodb 数据库基本操作,包含最常用的 15 条命令,非常适合MongoDB入门级学习使用,文件绿色安全,仅供学习交流使用,无任何商业目的,欢迎大家下载使用!
在MongoDB中,数据是以文档的形式存储的,文档是一个键值对的集合,并且文档支持嵌套和数组。MongoDB中的一个文档相当于关系型数据库中的一行,多个文档构成了集合(collection),集合类似于关系型数据库中的表。 ...
MongoDB Plugin可能java上最好用而且实时跟进官方版本的ORM,让您更加舒适地使用MongoDB。 与MongoDB版本基本同步(基于最新3.4.0) 使用方式更像是自然语言,用起来更舒适 几乎没有任何学习成本 支持MongoDB...
本文从基本概念,数据逻辑结构、存储结构,功能特性,系统架构,应用场景等方面详细的介绍了MongoDB,并将MongoDB与MySQL操作命令对比,并附加了MongoDB爱好者交流QQ群
6. 本文件目录为功能代码块,可参考笔者的一篇MongoDB相关的博客使用,包含作者踩过的一些坑,可帮助初学使用者加深理解,内容涉及MongoDB相关(安装,常识,Mysql和MongoDB的基本对比,原生mongo shell命令...) ...