- 论坛徽章:
- 0
|
本帖最后由 liyihongcug 于 2011-08-04 15:34 编辑
http://blogs.ejb.cc/archives/227NoSQL特点
1、 它可以处理超大量的数据
2、 它运行在便宜的PC服务器集群上
PC 集群扩充起来非常方便并且成本很低,避免了传统商业数据库“sharding”操作的复杂性
和成本。
3、 它击碎了性能瓶颈
NoSQL 的支持者称,通过NoSQL 架构可以省去将Web 或Java 应用和数据转换成SQL 格式的
时间,执行速度变得更快。
“SQL 并非适用于所有的程序代码”,对于那些繁重的重复操作的数据,SQL 值得花钱。但
是当数据库结构非常简单时,SQL 可能没有太大用处。
4、 它没有过多的操作
虽然NoSQL 的支持者也承认关系型数据库提供了无可比拟的功能集合,而且在数据完整性
上也发挥绝对稳定,他们同时也表示,企业的具体需求可能没有那么复杂。
5、 它的支持者源于社区
因为NoSQL 项目都是开源的,因此它们缺乏供应商提供的正式支持。这一点它们与大多数
开源项目一样,不得不从社区中寻求支持。
NoSQL 发展至今,出现了好几种非关系性数据库, 以NoSQL 中目前表现最好的
MongoDB 。
功能测试
j = { name : "ray_linn" };
{ "name" : "ray_linn" }
> t = { x : 3 };
{ "x" : 3 }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
{ "_id" : ObjectId("4e3a416556994ac1a0aa610f"), "x" : 3 }
> for (var i = 1; i <= 20; i++) db.things.save({x : 4, j : i});----------------------------请注意 这里“字段名” 必须是{} 函数是()
> db.things.find();
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
{ "_id" : ObjectId("4e3a416556994ac1a0aa610f"), "x" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6110"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6111"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6112"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6113"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6114"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6115"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6116"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6117"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6118"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6119"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611a"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611b"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611c"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611d"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611e"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611f"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6120"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6121"), "x" : 4, "j" : 18 }
has more
> var cursor = db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());----------------------------------------------------------使用游标 while循环
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
{ "_id" : ObjectId("4e3a416556994ac1a0aa610f"), "x" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6110"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6111"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6112"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6113"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6114"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6115"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6116"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6117"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6118"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6119"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611a"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611b"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611c"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611d"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611e"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611f"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6120"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6121"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6122"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6123"), "x" : 4, "j" : 20 }
> db.things.find().forEach(printjson);--------------------------------------------------------------------使用forEach mongo区分大小写 如果错误哦会提示
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
{ "_id" : ObjectId("4e3a416556994ac1a0aa610f"), "x" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6110"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6111"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6112"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6113"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6114"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6115"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6116"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6117"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6118"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6119"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611a"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611b"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611c"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611d"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611e"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa611f"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6120"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6121"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6122"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6123"), "x" : 4, "j" : 20 }
> var cursor = db.things.find();
> printjson(cursor[4]); --------------------------------------------------------------------- 游标数组应用
{ "_id" : ObjectId("4e3a437956994ac1a0aa6112"), "x" : 4, "j" : 3 }
> db.things.find({name:"ray_linn"}).forEach(printjson); -----------------------------------相当于 select * from a where a='d'
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
> db.things.find({j:"19"}).forEach(printjson);
> db.things.find({j:19}).forEach(printjson);
{ "_id" : ObjectId("4e3a437956994ac1a0aa6122"), "x" : 4, "j" : 19 }
--------------------------------------------注意整型是不要"" 这里多个字段 。如果只显示某个字段 可以让 db.things.find({x:4}, {j:true}).forEach(printjson);
> db.things.find({x:4,j:19}).forEach(printjson);
{ "_id" : ObjectId("4e3a437956994ac1a0aa6122"), "x" : 4, "j" : 19 }
> printjson(db.things.findOne({name:"mongo"});
...
... exit;
... ^C-----------------------------------------------------------------输入错误的提示信息
> printjson(db.things.findOne({name:"ray_linn"})); 相当于mysql 的 limit 1
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
> db.things.find().limit(3);
{ "_id" : ObjectId("4e3a416156994ac1a0aa610e"), "name" : "ray_linn" }
{ "_id" : ObjectId("4e3a416556994ac1a0aa610f"), "x" : 3 }
{ "_id" : ObjectId("4e3a437956994ac1a0aa6110"), "x" : 4, "j" : 1 }
> show collections;
system.indexes
things
usr
> printjson
function (x) {
print(tojson(x));
}
1
补充重要的一点 这个是自己插入主键
使用游标时候请注意占用内存的问题, 特别是很大的游标对象, 有可能会内存溢出. 所以应
该用迭代的方式来输出. 下面的示例则是把游标转换成真实的数组类型:
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
请注意这些特性只是在MongoDB shell 里使用, 而不是所有的其他应用程序驱动都支持.
MongoDB 游标对象不是没有快照,如果有其他用户在集合里第一次或者最后一次调用
next(), 你可能得不到游标里的数据. 所以要明确的锁定你要查询的游标
db.things.insert({_id:3, name:"Bill_new", age:55})
db.things.update({name:"mongo"},{$set:{name:"mongo_new"}});
db.things.remove({name:"mongo_new"});
MongoVUE
主页:http://www.mongovue.com/
一个桌面程序,提供了对MongoDB 数据库的基本操作,如查看、查询、更新、删除等,简
单易用,但是功能还比较弱,以后发展应该不错 |
|