一、Mongodb的优点:
l 支持地理空间索引
l 存取JavaScript的函数和值
l 支持MapReduce和其他聚合工具
二、Mongodb的概念
【1】mogodb中的文档
l 多个键及其关联的值有序地放在一起就是文档
【2】文档的键和值
l 文档的键-可以是任意utf-8字符串,除了\0,_,$
l 文档的值-其他数据类型(包括文档)
【3】文档注意事项:
l 文档中不能有重复的键值
l 文档区分数据类型
l 文档区分大小写
【4】集合
l 集合是一组文档
l 集合是无模式的
l 有很多理由,创建一个模式,将相同类型的文档规整到一起。比如索引就更高效
【5】数据库
l 多个集合可以组成数据库
l 一个mongodb实例可以承载多个数据库
l 在磁盘上,不同数据库放在不同的文件上
【6】保留数据库
l admin
继承了所有数据库的权限;比如关闭所有的数据库或者关闭服务器
只能从这个用户执行
l config
在mongodb分片的时候,config数据库存放分片的相关信息
三、Mongodb的启动
由于
(1)[main] 32-bit servers don't have journal
ing enabled by default. Please use --journal if you want durability.
(2)exception in initAndList
en: 28663 Cannot start server. The default storage engine 'wiredTiger' is not av
ailable with this build of mongod. Please specify a different storage engine exp
licitly, e.g. --storageEngine=mmapv1., terminating
所以启动参数为:
mongod --dbpath=D:\MongoDB\data --journal --storageEngine=mmapv1
启动之后,显示
2016-10-09T11:48:11.522+0800 I NETWORK [initandlisten] waiting for connections on port 27017
2016-10-09T13:09:59.392+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9524 #1 (1 connection now open)
2016-10-09T13:09:59.639+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9525 #2 (2 connections now open)
2016-10-09T13:44:00.346+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9559 #3 (3 connections now open)
2016-10-09T13:44:00.431+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9560 #4 (4 connections now open)
2016-10-09T13:44:16.044+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9561 #5 (5 connections now open)
2016-10-09T13:44:16.109+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9562 #6 (6 connections now open)
2016-10-09T15:54:06.653+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9745 #7 (7 connections now open)
2016-10-09T15:54:06.726+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:9746 #8 (8 connections now open)
2016-10-10T12:24:39.512+0800 I NETWORK [conn8] end connection 127.0.0.1:9746 (7 connections now open)
启动shell,mongo
D:\MongoDB\Server\3.2\bin>mongo
2016-09-29T09:28:34.602+0800 I CONTROL [main] Hotfix KB2731284 or later update is installed, no need to zero-out data files
MongoDB shell version: 3.2.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
推荐使用runmogo工具客户端
四、Mongodb的使用
【1】javaScript函数
function f(n){
if (n <= 1)
return 1;
else
return n*f(n-1);
}
f(4)=24
【2】使用js进行数据的遍历
| var collections=["title","content","comments"]; for (i in collections){ print(db.blog[collections[i]]); } |
【3】shell的使用--插入更新
| post={ "title":"my blog post", "content":"here is my blog post", "date":new Date() }
/* 1 */ { "title" : "my blog post", "content" : "here is my blog post", "date" : ISODate("2016-10-09T06:51:54.710Z") } |
| db.blog.insert(post)
db.blog.find()
/* 1 */ { "_id" : ObjectId("57f9eabbd01d44ba3aaeb26c"), "title" : "my blog post", "content" : "here is my blog post", "date" : ISODate("2016-10-09T06:51:54.710Z") } |
| db.blog.update({title:"my blog post"},post)
/* 1 */ { "_id" : ObjectId("57f9eabbd01d44ba3aaeb26c"), "title" : "my blog post", "content" : "here is my blog post", "date" : ISODate("2016-10-09T06:51:54.710Z"), "comments" : [ "test blog" ] }
|
| var a=db.blog.findOne(); a; a.content='update'; db.blog.update({"title" : "my blog post"},a); db.blog.find(); delete a.comments; db.blog.update({"title" : "my blog post"},a); |
【4】shell的使用--查看js对象
| db.blog.insert
function ( obj , options, _allow_dot ){ if ( ! obj ) throw Error( "no object passed to insert!" );
var flags = 0;
var wc = undefined; var allowDottedFields = false; if ( options === undefined ) { // do nothing } else if ( typeof(options) == 'object' ) { if (options.ordered === undefined) { //do nothing, like above } else { flags = options.ordered ? 0 : 1; }
if (options.writeConcern) wc = options.writeConcern; if (options.allowdotted) allowDottedFields = true; } else { flags = options; }
// 1 = continueOnError, which is synonymous with unordered in the write commands/bulk-api var ordered = ((flags & 1) == 0);
if (!wc) wc = this.getWriteConcern();
var result = undefined; var startTime = (typeof(_verboseShell) === 'undefined' || !_verboseShell) ? 0 : new Date().getTime();
if ( this.getMongo().writeMode() != "legacy" ) { // Bit 1 of option flag is continueOnError. Bit 0 (stop on error) is the default. var bulk = ordered ? this.initializeOrderedBulkOp() : this.initializeUnorderedBulkOp(); var isMultiInsert = Array.isArray(obj);
if (isMultiInsert) { obj.forEach(function(doc) { bulk.insert(doc); }); } else { bulk.insert(obj); }
try { result = bulk.execute(wc); if (!isMultiInsert) result = result.toSingleResult(); } catch( ex ) { if ( ex instanceof BulkWriteError ) { result = isMultiInsert ? ex.toResult() : ex.toSingleResult(); } else if ( ex instanceof WriteCommandError ) { result = isMultiInsert ? ex : ex.toSingleResult(); } else { // Other exceptions thrown throw Error(ex); } } } else { if ( ! _allow_dot ) { this._validateForStorage( obj ); }
if ( typeof( obj._id ) == "undefined" && ! Array.isArray( obj ) ){ var tmp = obj; // don't want to modify input obj = {_id: new ObjectId()}; for (var key in tmp){ obj[key] = tmp[key]; } }
this.getMongo().insert( this._fullName , obj, flags );
// enforce write concern, if required if (wc) result = this.runCommand("getLastError", wc instanceof WriteConcern ? wc.toJSON() : wc); }
this._lastID = obj._id; this._printExtraInfo("Inserted", startTime); return result; } |
五、Mongodb的数据类型
【1】json:www.json.org仅包含六种数据类型;
l null,布尔,数字,字符串,数组,对象
l json没有日期类型,使得日期处理变得繁琐
l 只有一种数字类型,不能区分浮点数和整数,32位,64位数
l 不支持正则表达式和函数类型
【2】mongodb保留json的基本键值对特性
l null
l 布尔:true,false
l 32位整数
l 64位整数
l 64位浮点数--默认的数字类型
l 字符串
l 符号
l 对象id,12字节,ObjectId()
l 日期:存放毫秒数,不存储时区 new Date()
l 正则表达式:
{"x":/foobar/i}
l 代码,存放js代码
{"x":function(){/******/}}
l 二进制数据
l 最大值:bson
l 最小值:bson
l 未定义:undefined
{"x":undifined}
l 数组:
{ "x" : ["a","b","c"] }
l 内嵌文档
{"x" : {"fool" :"bar"} }

12万+

被折叠的 条评论
为什么被折叠?



