如何检查当前mongodb是否启用了WiredTiger存储引擎?

 

如何检查当前mongodb是否启用了WiredTiger存储引擎?

可以至少通过以下2种方法 验证:

1、在Linux/OSX上执行如下的命令

 

 

WIREDTIGER_CONFIGURED=`ps -ef|grep mongod|grep -i storageengine|grep -ic wiredtiger`
echo ${WIREDTIGER_CONFIGURED}

 

 

如果返回为1则说明当前系统中运行着一个以WiredTiger为存储引擎的mongod

 

2、在Linux/OSX上执行如下的命令

 

echo "db.serverStatus()"| mongo|grep wiredTiger

若返回信息中有wiredTiger,则说明该mongo连接到了一个启用了wiredTiger存储引擎的mongod.

注意对于启用了wiredTiger的文件路径–dbpath,无法再使用默认mmapv1存储引擎打开,例如:

 

ac:mongodata maclean$ mongod --storageEngine wiredTiger --dbpath  /Users/maclean/mongodata
2015-05-06T11:35:32.372+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2015-05-06T11:35:32.997+0800 I CONTROL  [initandlisten] MongoDB starting : pid=16590 port=27017 dbpath=/Users/maclean/mongodata 64-bit host=ac.local
2015-05-06T11:35:32.998+0800 I CONTROL  [initandlisten] db version v3.0.2
2015-05-06T11:35:32.998+0800 I CONTROL  [initandlisten] git version: nogitversion
2015-05-06T11:35:32.998+0800 I CONTROL  [initandlisten] build info: Darwin yosemitevm.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-05-06T11:35:32.998+0800 I CONTROL  [initandlisten] allocator: system
2015-05-06T11:35:32.999+0800 I CONTROL  [initandlisten] options: { storage: { dbPath: "/Users/maclean/mongodata", engine: "wiredTiger" } }
2015-05-06T11:35:33.045+0800 I NETWORK  [initandlisten] waiting for connections on port 27017


如上启用了--storageEngine wiredTiger 可以正常运行



如果用mmapv1引擎则报错

ac:mongodata maclean$ mongod --storageEngine mmapv1 --dbpath  /Users/maclean/mongodata
2015-05-06T11:36:34.037+0800 I STORAGE  [initandlisten] exception in initAndListen: 28574 Cannot start server. Detected data files in /Users/maclean/mongodata created by storage engine 'wiredTiger'. The configured storage engine is 'mmapv1'., terminating
2015-05-06T11:36:34.037+0800 I CONTROL  [initandlisten] dbexit:  rc: 100

mongodb db.collection.remove用法

 

对于db.collection.remove需要加入加入一个query才能正常运行,否则仅仅运行remove()将会报错,例如:

 

> db.dbdao_stuff.insert({"_id":1,"a":1,"b":1});
WriteResult({ "nInserted" : 1 })
> 
> db.dbdao_stuff.insert({"_id":2,"a":2,"b":3});
WriteResult({ "nInserted" : 1 })
> db.dbdao_stuff.insert({"_id":3,"a":3,"b":6});
WriteResult({ "nInserted" : 1 })
> db.dbdao_stuff.insert({"_id":4,"a":4,"b":10});
WriteResult({ "nInserted" : 1 })
> db.dbdao_stuff.insert({"_id":5,"a":5,"b":15});
WriteResult({ "nInserted" : 1 })
> 
> db.dbdao_stuff.find();
{ "_id" : 1, "a" : 1, "b" : 1 }
{ "_id" : 2, "a" : 2, "b" : 3 }
{ "_id" : 3, "a" : 3, "b" : 6 }
{ "_id" : 4, "a" : 4, "b" : 10 }
{ "_id" : 5, "a" : 5, "b" : 15 }


db.dbdao_stuff.remove();
2015-05-05T21:20:21.275+0800 E QUERY    Error: remove needs a query
    at Error ()
    at DBCollection._parseRemove (src/mongo/shell/collection.js:305:32)
    at DBCollection.remove (src/mongo/shell/collection.js:328:23)
    at (shell):1:16 at src/mongo/shell/collection.js:305


如上语句 仅仅运行 db.dbdao_stuff.remove(); 会报错 Error: remove needs a query
> db.dbdao_stuff.remove({});
WriteResult({ "nRemoved" : 5 })

这时候加入 remove({}); 即可删除所有document

 

 

db.collection.remove有以下2种用法:

 

db.collection.remove(
 <query>,
 <justOne>
)


或者 

db.collection.remove(
 <query>,
 {
 justOne: <boolean>,
 writeConcern: <document>
 }
)

justone=>TRUE 时仅仅删除一条符合条件的document , writeConcern是关于safe write的。

MongoDB $unset重置某个field

对于已经有值的field,在mongodb中可以使用$unset操作符来重置该field。

http://docs.mongodb.org/manual/reference/operator/update/unset/

具体语法如下:

 

{ $unset: { <field1>: "", ... } }
例如
db.products.update(
 { sku: "unknown" },
 { $unset: { quantity: "", instock: "" } }
)

 

 

具体使用的例子:

db.dbdao_sample.insert({"_id":3,"a":7,"c":[3,4,7,-1,-1]});


> db.dbdao_sample.update({"_id":3},{"$unset" : {"c":""}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dbdao_sample.find();
{ "_id" : 3, "a" : 7 }


db.dbdao_sample.insert({"_id":4,"a":7,"c":[3,4,7,-1,-1]});


db.dbdao_sample.update({"_id":4},{"$remove" : {"c":""}});

没有$remove这个操作符

> db.dbdao_sample.update({"_id":4},{"c": {"$delete":true }});
2015-05-05T20:38:33.817+0800 E QUERY Error: field names cannot start with $ [$delete]
 at Error (<anonymous>)
 at DBCollection._validateForStorage (src/mongo/shell/collection.js:161:19)
 at DBCollection._validateForStorage (src/mongo/shell/collection.js:165:18)
 at DBCollection._validateUpdateDoc (src/mongo/shell/collection.js:388:14)
 at Object.findOperations.updateOne (src/mongo/shell/bulk_api.js:675:20)
 at DBCollection.update (src/mongo/shell/collection.js:455:22)
 at (shell):1:17 at src/mongo/shell/collection.js:161

> db.dbdao_sample.update({"_id":4},{"c":null});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> 
> db.dbdao_sample.find({"_id":4});
{ "_id" : 4, "c" : null }


> db.dbdao_sample.update({"_id":4},{"c":1});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dbdao_sample.find({"_id":4});
{ "_id" : 4, "c" : 1 }


mongodb $all操作符

$all操作符帮助选择collection中field是array类型的,且该array包含所有指定的元素的document。 若使用$all操作符,语法如下:

 

{ <field>: { $all: [ <value1> , <value2> ... ] } }

 

使用例子如下

 

> db.dbdao_col.insert({_id:1,"a":1,"b":[3]});
WriteResult({ "nInserted" : 1 })
> db.dbdao_col.insert({_id:11,"a":"5","b":[5,3,7]});
WriteResult({ "nInserted" : 1 })
> db.dbdao_col.insert({_id:111,"a": 1 ,"b":[3,5]});
WriteResult({ "nInserted" : 1 })
> db.dbdao_col.insert({_id:12,"a": "5" ,"b":["alpha",5]});
WriteResult({ "nInserted" : 1 })

> db.dbdao_col.find();
{ "_id" : 1, "a" : 1, "b" : [ 3 ] }
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }
{ "_id" : 12, "a" : "5", "b" : [ "alpha", 5 ] }


> db.dbdao_col.find({"b": { "$all" : [5,3]}});
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }
> 
> 
> db.dbdao_col.find({"b": { "$all" : [3,5]}});
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }

> db.dbdao_col.insert({_id:1,"a":1,"b":[3]});
WriteResult({ "nInserted" : 1 })
> db.dbdao_col.insert({_id:11,"a":"5","b":[5,3,7]});
WriteResult({ "nInserted" : 1 })
> db.dbdao_col.insert({_id:111,"a": 1 ,"b":[3,5]});
WriteResult({ "nInserted" : 1 })
> db.dbdao_col.insert({_id:12,"a": "5" ,"b":["alpha",5]});
WriteResult({ "nInserted" : 1 })

> db.dbdao_col.find();
{ "_id" : 1, "a" : 1, "b" : [ 3 ] }
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }
{ "_id" : 12, "a" : "5", "b" : [ "alpha", 5 ] }


> db.dbdao_col.find({"b": { "$all" : [5,3]}});
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }
> 
> 
> db.dbdao_col.find({"b": { "$all" : [3,5]}});
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }

 

 

$all 其实等价于$and操作符的这种用法,但是写起来太繁琐:

 

> db.dbdao_col.find({ $and:[ {"b":3},{"b":5}]});
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }


> db.dbdao_col.find({ $and:[ {"b":5},{"b":3}]});
{ "_id" : 11, "a" : "5", "b" : [ 5, 3, 7 ] }
{ "_id" : 111, "a" : 1, "b" : [ 3, 5 ] }

MongoDB db.collection.remove()方法

mongodb中删除document采用remove方法,

http://docs.mongodb.org/manual/reference/method/db.collection.remove/

db.collection.remove(),从一个collection中移除对应的document。db.collection.remove()可以有2种用法:

 

 

db.collection.remove(
 <query>,
 <justOne>
)

justone是布尔类型,是可选的参数,来保证只删除一个document; 默认为false即删除所有符合要求的document>

从版本2.6开始加入了新的语法:

db.collection.remove(
   ,
   {
     justOne: ,
     writeConcern: 
   }
)

 

writeConcern 关于safe write。

下面的是db.collection.remove()的使用例子

 

其中new Date方法用来返回一个mongoldb中的ISOdate类型

 

 

ac:~ maclean$ mongo
MongoDB shell version: 3.0.2
connecting to: test
> 
> 
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-01-01")});
WriteResult({ "nInserted" : 1 })
> 
> 
> 
> 
> db.dbdao_email.find();
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("1999-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("2001-02-01")});
WriteResult({ "nInserted" : 1 })
> db.dbdao_email.insert({email_title:"dbdao email!",date: new Date("2002-02-01")});
WriteResult({ "nInserted" : 1 })
> 
> 
> db.dbdao_email.find({date:{ $lt : new Date("2000-01-01")}});
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
{ "_id" : ObjectId("554766e4e90479237b67c783"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e5e90479237b67c784"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e6e90479237b67c785"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
> 
> db.dbdao_email.find();
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
{ "_id" : ObjectId("554766e4e90479237b67c783"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e5e90479237b67c784"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e6e90479237b67c785"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766ece90479237b67c786"), "email_title" : "dbdao email!", "date" : ISODate("2001-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766efe90479237b67c787"), "email_title" : "dbdao email!", "date" : ISODate("2002-02-01T00:00:00Z") }
> 
> db.dbdao_email.find({date:{ $lt : new Date("2000-01-01")}});
{ "_id" : ObjectId("554766d2e90479237b67c782"), "email_title" : "dbdao email!", "date" : ISODate("1999-01-01T00:00:00Z") }
{ "_id" : ObjectId("554766e4e90479237b67c783"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e5e90479237b67c784"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766e6e90479237b67c785"), "email_title" : "dbdao email!", "date" : ISODate("1999-02-01T00:00:00Z") }
> 
> db.dbdao_email.remove({date:{ $lt : new Date("2000-01-01")}})
WriteResult({ "nRemoved" : 4 })
> db.dbdao_email.find({date:{ $lt : new Date("2000-01-01")}});
> db.dbdao_email.find();
{ "_id" : ObjectId("554766ece90479237b67c786"), "email_title" : "dbdao email!", "date" : ISODate("2001-02-01T00:00:00Z") }
{ "_id" : ObjectId("554766efe90479237b67c787"), "email_title" : "dbdao email!", "date" : ISODate("2002-02-01T00:00:00Z") }

有的时候为了减少存储使用,用户决定删除collection中的部分记录,则可以用db.emails.remove({“date”: {“lt”: new Date(“2001-01-01”)}})

了解db.collection.find()

db.collection.find()可能是mongodb中最常用的方法之一了,其定义为db.collection.find(query, projection), 即查询一个collection中的文档并返回一个包含了选定文件(document)的字段(field)的游标。

 

参数   query : 是可选参数,基于查询操作符指定了查询的条件,若希望返回collection中的所有文件,则无需指定该query 参数,直接 db.collection.find()即可

参数 projection:是可选参数,指定了那些字段是需要返回的,若要返回所有字段则不必要指定该参数。

 

projection参数的形式如下:

{ field1: <boolean>, field2: <boolean> ... }


如上boolean布尔类型可以是下面的值:

  • 1 或者 true 代表包含该field。find()方法总是包括_id字段,即便你这里没有指定要_id字段。
  • 0或者false 代表不不包含该field。

 

例如:

> db.dbdao_student.insert({name:"maclean",age:30,score:100});
WriteResult({ "nInserted" : 1 })
> db.dbdao_student.insert({name:"jiang",age:20,score:100});
WriteResult({ "nInserted" : 1 })
>
>
>
> db.dbdao_student.find({},{age:1});
{ "_id" : ObjectId("55461b72fefe5936ccc41386"), "age" : 30 }
{ "_id" : ObjectId("55461b7cfefe5936ccc41387"), "age" : 20 }

一个更复杂一些的例子:

 

对于给定的collection, tab1

> db.dbdao_tab1.insert({a:0,b:0,c:0});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:0,b:0,c:0});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:1,b:1,c:1});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:1,b:2,c:1});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:2,b:2,c:2});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:2,b:2,c:2});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:3,b:3,c:3});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:3,b:3,c:3});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:4,b:4,c:4});
WriteResult({ "nInserted" : 1 })
> db.dbdao_tab1.insert({a:4,b:4,c:4});
WriteResult({ "nInserted" : 1 })

> db.dbdao_tab1.find()
{ "_id" : ObjectId("554727b8ce7f1dc05695adad"), "a" : 0, "b" : 0, "c" : 0 }
{ "_id" : ObjectId("554727bdce7f1dc05695adae"), "a" : 0, "b" : 0, "c" : 0 }
{ "_id" : ObjectId("554727c7ce7f1dc05695adaf"), "a" : 1, "b" : 1, "c" : 1 }
{ "_id" : ObjectId("554727ccce7f1dc05695adb0"), "a" : 1, "b" : 2, "c" : 1 }
{ "_id" : ObjectId("554727d6ce7f1dc05695adb1"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727dfce7f1dc05695adb2"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727e7ce7f1dc05695adb3"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727eece7f1dc05695adb4"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727f6ce7f1dc05695adb5"), "a" : 4, "b" : 4, "c" : 4 }
{ "_id" : ObjectId("554727fbce7f1dc05695adb6"), "a" : 4, "b" : 4, "c" : 4 }

对于查询db.dbdao_tab1.find( {a:1,b:2} , {a:1} ),会返回多少个document

> db.dbdao_tab1.find( {a:1,b:2} , {a:1} ); 
{ "_id" : ObjectId("554727ccce7f1dc05695adb0"), "a" : 1 }


 

Screen Shot 2015-05-04 at 4.12.18 PM

例子

返回一个collection中的所有document

find()方法不加任何参数即从一个collection中返回所有的document 包括了所有的field。 举例来说,下面的操作返回了dbdao中所有的文档:

db.dbdao.find()

查找符合匹配查询条件的document

如下面的例子查找dbdao_products 中qty > 25的document

db.dbdao_products.find( { qty: { $gt: 25 } } )

 

> db.dbdao_tab1.find( { a : { $gt :1} } );
{ "_id" : ObjectId("554727d6ce7f1dc05695adb1"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727dfce7f1dc05695adb2"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727e7ce7f1dc05695adb3"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727eece7f1dc05695adb4"), "a" : 3, "b" : 3, "c" : 3 }
{ "_id" : ObjectId("554727f6ce7f1dc05695adb5"), "a" : 4, "b" : 4, "c" : 4 }
{ "_id" : ObjectId("554727fbce7f1dc05695adb6"), "a" : 4, "b" : 4, "c" : 4 }
> 

> db.dbdao_tab1.find( { a : { $in : [ 1 ,2 ] } });
{ "_id" : ObjectId("554727c7ce7f1dc05695adaf"), "a" : 1, "b" : 1, "c" : 1 }
{ "_id" : ObjectId("554727ccce7f1dc05695adb0"), "a" : 1, "b" : 2, "c" : 1 }
{ "_id" : ObjectId("554727d6ce7f1dc05695adb1"), "a" : 2, "b" : 2, "c" : 2 }
{ "_id" : ObjectId("554727dfce7f1dc05695adb2"), "a" : 2, "b" : 2, "c" : 2 }

 

使用操作符的查询

下面的查询返回dbdao_tab3集合中_id=5或者或者为ObjectId(“5547301bdda6d7285bef03f7”)的记录

 

> db.dbdao_tab3.find();
{ "_id" : 432424, "a" : 1 }
{ "_id" : ObjectId("5547301bdda6d7285bef03f7"), "a" : 10 }

> db.dbdao_tab3.find(
... { _id : { $in : [432424,ObjectId("5547301bdda6d7285bef03f7") ] }
... }
... )
{ "_id" : 432424, "a" : 1 }
{ "_id" : ObjectId("5547301bdda6d7285bef03f7"), "a" : 10 }




 

范围查询

组合使用比较操作符来实现范围查询,下面的操作会返回field在value1 和 value2之间的document:

db.collection.find( { field: { $gt: value1, $lt: value2 } } );

MongoDB的技术优势

任何关系型数据库RDBMS均会有典型的Schema Design模式设计来规划系统中有多少张表以及这些表之间的关联。但在MongoDB中没有关系relationship 的概念

MongoDB对比RDBMS关系型数据库的优势

 

  • schema less:MongoDB是基于文档存储的数据库,在MongoDB中一个collection集合中可以存放多个不同的文档。 文档与文档之间对比,字段的数量,字段的和大小都是可以不同的。
  • 单个对象的结构清楚
  • MongoDB通过不支持JOIN来深化其横向扩展能力
  • 深度查询能力。MongoDB通过基于文档的查询语言提供针对文档的动态查询语言,其查询能力接近于SQL语言。
  • 易于扩展,MongDB具备很好的扩展性
  • 对于数据库对象和应用对象之间的转换更少
  • 使用内部内存来排序工作集,让数据访问更快速

为什么使用MongoDB?

  • 面向文档的存储方式:数据被以JSON风格文档形式存放
  • 任何属性均可索引
  • 具有复制和高可用特性
  • 自动数据分片
  • 丰富的查询功能
  • 快速的数据库升级
  • 有mongodb(10gen)公司提供支持

 

何处可以使用MongoDB?

  • Big Data
  • 内容管理和发布
  • 移动和社交
  • 用户数据管理
  • Data HUB

MongoDB的一些优势

which of the following describe situations where mongodb has a clear advantage over a typical relational database? check all apply

  1. developer need to get an application into production quickily
  2. Queries from applications are ad hoc and unpredictable
  3. The data model changes frequently
  4. Many Long running complex transactions are present

 

由于mongodb是schema less的所以其初始开发速度异常的快, schema less意味着开发人员可以够少地做模式方案设计,更快将应用投产。

同时由于mongodb是schema less的,所以其数据模型变更几乎不需要做任何操作(alter table add/modify)。

对于ad hoc &unpredictable 特性的、无法预估的查询,mongodb对于字段搜索,范围查询、正则表达查询都支持。查询可以要求返回文件document的部分字段,同时也用上用户自定义的java script函数。

Ad hoc queries
MongoDB supports search by field, range queries, regular expression searches. Queries can return specific fields of documents and also include user-defined JavaScript functions. http://en.wikipedia.org/wiki/MongoDB

Many Long running complex transactions are present  ==》 显然 mongodb中并不适合运行大的长的事务

 

关于mongodb的一些特性

关于mongodb的一些特性:

 

 

Which features of relational database management systems were deliberately omitted in MongoDB and help us to obtain horizontal scalability ?  check all the apply.

 

  1. Authentication
  2. Joins
  3. multi-document transactions
  4. multi-statement transactions

 

Authentication

mongodb 支持Authentication 身份认证,一旦启用访问控制,mongodb将要求所有客户端在授权认证后才能访问; mongodb 3.0以后默认使用 challenge and response mechanism (SCRAM-SHA-1),之前的版本默认使用 MongoDB Challenge and Response (MONGODB-CR)。

mongodb也支持x509 certificate authentication, LDAP proxy authentication, and Kerberos authentication等授权认证。

http://docs.mongodb.org/manual/core/authentication/

 

Join

MongoDB wasn’t designed in a lab. We built MongoDB from our own experiences building large scale, high availability, robust systems. We didn’t start from scratch, we really tried to figure out what was broken, and tackle that. So the way I think about MongoDB is that if you take MySql, and change the data model from relational to document based, you get a lot of great features: embedded docs for speed, manageability, agile development with schema-less databases, easier horizontal scalability because joins aren’t as important.

mongodb的开发人员认为水平扩展能力horizontal scalability  要比JOIN来的重要,所以在mongodb 中并不直接支持join。

Significantly, MongoDB supports neither joins nor transactions。

 

 

Multi-ducoment transactions

 

MongoDB does not support multi-document transactions.

http://docs.mongodb.org/manual/faq/fundamentals/

However, MongoDB does provide atomic operations on a single document.

mongodb提供对单个document的原子性操作,但不支持涉及到多个文件的食物multi-document transactions。

 

 

multi-statement transactions

Pattern

Overview

Consider a scenario where you want to transfer funds from account A to account B. In a relational database system, you can subtract the funds from A and add the funds to B in a single multi-statement transaction. In MongoDB, you can emulate a two-phase commit to achieve a comparable result.

MongoDB不支持multi-statement transaction多语句事务,可以通过2段式提交来获得类似效果。

 

MongoDB _id Key的一些信息

关于 mongodb _id key:

  1. _id key可以用户分配,也可以由mongodb自动分配,一般采用自动分配。
  2. 如果未使用_id作为分片key,则应用程序或客户端层要负责保证_id为唯一的,对于collection存在重复_id会有问题。If you do not use _id as the shard key, then your application/client layer must be responsible for keeping the _id field unique. It is problematic for collections to have duplicate _id values.
  3. 更新一个document 不会造成_ID被修改

对于更新一个document 不会造成_ID被修改的证明:

> db.version();
3.0.2

> db.dbdao_student.insert({student_name:"maclean",age:20,sex:"male",score:100});
WriteResult({ "nInserted" : 1 })

> db.dbdao_student.update({student_name:"maclean"},{"student_name" : "maclean", "age" : 20, "sex" : "male", "score" : 100});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.dbdao_student.find();
{ "_id" : ObjectId("5545bcff5714fbdfcb483510"), "student_name" : "maclean", "age" : 20, "sex" : "male", "score" : 100 }
>
> db.dbdao_student.update({student_name:"maclean"},{"student_name" : "maclean", "age" : 21, "sex" : "male", "score" : 100});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dbdao_student.find();
{ "_id" : ObjectId("5545bcff5714fbdfcb483510"), "student_name" : "maclean", "age" : 21, "sex" : "male", "score" : 100 }
>
> db.dbdao_student.update({student_name:"maclean"},{"student_name" : "maclean", "age" : 25, "sex" : "male", "score" : 100});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.dbdao_student.find();
{ "_id" : ObjectId("5545bcff5714fbdfcb483510"), "student_name" : "maclean", "age" : 25, "sex" : "male", "score" : 100 }
>
> db.dbdao_student.update({student_name:"maclean"},{"student_name" : "maclean", "age" : 25});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.dbdao_student.find();
{ "_id" : ObjectId("5545bcff5714fbdfcb483510"), "student_name" : "maclean", "age" : 25 }

> db.dbdao_student.find(ObjectId("5545bcff5714fbdfcb483510"));
{ "_id" : ObjectId("5545bcff5714fbdfcb483510"), "student_name" : "maclean", "age" : 25 }

沪ICP备14014813号-2

沪公网安备 31010802001379号