nosql之java

三、JAVA语言操作MongoDB

客户端软件下载:http://www.mongovue.com/downloads/

安装:下载mongodb-win32-i386-1.7.1.zip(http://dl.mongodb.org/dl/win32/i386)或(http://www.mongodb.org/downloads)
新建目录:D:\mongodb
将mongodb-win32-i386-1.7.1.zip解压,将bin目录拷贝到D:\mongodb下,并在bin目录下建立data文件夹,

执行命令:mongod --dbpath data

运行MongoDBTest.java就可以了



在官方网站中下载mongo.jar,并添加到项目中。



创建类MongoDBTest.java



可以使用如下两种方式得到数据库连接对象:


Java代码 收藏代码

1. Mongo m1 = new Mongo();//默认本机连接
2. Mongo m2 = new Mongo("localhost", 27017);//连接地址,端口号

Mongo m1 = new Mongo();//默认本机连接
Mongo m2 = new Mongo("localhost", 27017);//连接地址,端口号



在创建连接对象之后,得到数据库:


Java代码 收藏代码

1. DB db = m.getDB("admin");//数据库名称:admin 如果数据库不存在 则自动创建

DB db = m.getDB("admin");//数据库名称:admin 如果数据库不存在 则自动创建



在得到数据库对象之后,得到表:


Java代码 收藏代码

1. DBCollection dbc = db.getCollection("things");//数据库admin下的表things 如没有此表 则自动创建

DBCollection dbc = db.getCollection("things");//数据库admin下的表things 如没有此表 则自动创建



mongoDB基于JAVA语言的CRUD ---



1.添加数据:


Java代码 收藏代码

1. DBObject o = new BasicDBObject();//创建一个对象
2. o.put("name", "iteye");//添加一个键值对
3. o.put("myname", "xiao9");//再添加一个键值对
4.
5. dbc.insert(o);//插入数据

DBObject o = new BasicDBObject();//创建一个对象
o.put("name", "iteye");//添加一个键值对
o.put("myname", "xiao9");//再添加一个键值对

dbc.insert(o);//插入数据



2.查询数据


Java代码 收藏代码

1. DBCursor c = dbc.find();//查询所有列表
2. List<DBObject> list = c.toArray();
3. for (int i = 0; i <list.size(); i++) {
4. DBObject dbo = list.get(i);
5. System.out.println(dbo.toString());
6. }

DBCursor c = dbc.find();//查询所有列表
List<DBObject> list = c.toArray();
for (int i = 0; i <list.size(); i++) {
DBObject dbo = list.get(i);
System.out.println(dbo.toString());
}


Java代码 收藏代码

1. DBObject o = new BasicDBObject();
2. o.put("name", "iteye");
3.
4. DBCursor c = dbc.find(o);//根据条件查询列表 (name=iteye)

DBObject o = new BasicDBObject();
o.put("name", "iteye");

DBCursor c = dbc.find(o);//根据条件查询列表 (name=iteye)


Java代码 收藏代码

1. DBObject o = dbc.findOne();//查询第一个数据

DBObject o = dbc.findOne();//查询第一个数据


Java代码 收藏代码

1. DBObject o = new BasicDBObject();
2. o.put("name", "iteye");
3.
4. DBObject o = dbc.findOne(o);//根据条件查询单个数据

DBObject o = new BasicDBObject();
o.put("name", "iteye");

DBObject o = dbc.findOne(o);//根据条件查询单个数据



3.修改数据


Java代码 收藏代码

1. DBObject queryObject = new BasicDBObject();
2. queryObject.put("name", "iteye");
3.
4. DBObject obj = new BasicDBObject();
5. queryObject.put("name", "iteye123");
6.
7. dbc.update(queryObject, obj);//查询条件,要修改的值

DBObject queryObject = new BasicDBObject();
queryObject.put("name", "iteye");

DBObject obj = new BasicDBObject();
queryObject.put("name", "iteye123");

dbc.update(queryObject, obj);//查询条件,要修改的值



4.删除数据


Java代码 收藏代码

1. DBObject obj = new BasicDBObject();
2. queryObject.put("name", "iteye123");
3.
4. dbc.remove(obj);//根据条件删除数据

DBObject obj = new BasicDBObject();
queryObject.put("name", "iteye123");

dbc.remove(obj);//根据条件删除数据



四、PHP语言操作MongoDB

Java代码 收藏代码

1. <?php
2.
3. //得到MongoDB连接
4. $m = new Mongo();
5.
6. //选择数据库comedy
7. $db = $m->comedy;
8.
9. //选择一个表 如没有此表则自动创建
10. $collection = $db->cartoons;
11.
12. //创建一个对象
13. $obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
14.
15. //插入对象到数据库
16. $collection->insert($obj);
17.
18. //创建一个对象
19. $obj = array( "title" => "XKCD", "online" => true );
20.
21. //插入对象到数据库
22. $collection->insert($obj);
23.
24. //查询所有该表中的对象
25. $cursor = $collection->find();
26.
27. //进行遍历和输出
28. foreach ($cursor as $obj) {
29. echo $obj["title"] . "\n";
30. }
31.
32.
33. //PHP也支持这种得到单个对象的API
34. $obj = $collection->findOne();
35. var_dump( $obj );
36.
37. //也可以进行循环插入
38. for($i=0; $i<100; $i++) {
39. $collection->insert( array( "i" => $i ) );
40. }
41.
42. //输出表中所有数据的数量
43. echo $collection->count();
44.
45. //PHP的条件查询
46. $query = array( "i" => 71 );
47. $cursor = $collection->find( $query );
48.
49. while( $cursor->hasNext() ) {
50. var_dump( $cursor->getNext() );
51. }
52.
53. //索引的建立
54. $coll->ensureIndex( array( "i" => 1 ) ); // create index on "i"
55. $coll->ensureIndex( array( "i" => -1, "j" => 1 ) ); // index on "i" descending, "j" ascending
56.
57. ?>

<?php

//得到MongoDB连接
$m = new Mongo();

//选择数据库comedy
$db = $m->comedy;

//选择一个表 如没有此表则自动创建
$collection = $db->cartoons;

//创建一个对象
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );

//插入对象到数据库
$collection->insert($obj);

//创建一个对象
$obj = array( "title" => "XKCD", "online" => true );

//插入对象到数据库
$collection->insert($obj);

//查询所有该表中的对象
$cursor = $collection->find();

//进行遍历和输出
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
}


//PHP也支持这种得到单个对象的API
$obj = $collection->findOne();
var_dump( $obj );

//也可以进行循环插入
for($i=0; $i<100; $i++) {
$collection->insert( array( "i" => $i ) );
}

//输出表中所有数据的数量
echo $collection->count();

//PHP的条件查询
$query = array( "i" => 71 );
$cursor = $collection->find( $query );

while( $cursor->hasNext() ) {
var_dump( $cursor->getNext() );
}

//索引的建立
$coll->ensureIndex( array( "i" => 1 ) ); // create index on "i"
$coll->ensureIndex( array( "i" => -1, "j" => 1 ) ); // index on "i" descending, "j" ascending

?>







五、对于MongoDB的安全设置,用户密码策略



MongoDB默认是不要求用户名和密码登陆的,这样并不安全,接下来就要设置登陆账号密码了。



(1)控制台设置用户密码和控制台通过用户密码访问MongoDB



1. 启动MongoDB服务器

cd d:

cd mongodb\bin

mongod --dbpath data



2. 打开一个新的CMD运行

cd d:

cd mongodb\bin

//打开mongodb数据库操作

mongo.exe

//使用admin库

use admin;

//添加登陆账号:user1 密码pwd1

db.addUser('user1','pwd1');



//查看是否设置成功

//db.system.users.find();



3. 关闭MongoDB服务器,并使用验证模式 ( auth )重新启动

cd d:

cd mongodb\bin

mongod --dbpath data --auth



接下来在通过CMD运行Mongodb的时候 就需要

cd d:

cd mongodb\bin

mongo.exe

use admin;

//进行登陆验证,如果不通过,是没有操作权限的了。

db.auth('user1','pwd1');



(2)JAVA方式通过用户密码访问MongoDB


Java代码 收藏代码

1. Mongo m = new Mongo();
2.
3. DB db = m.getDB("admin");
4.
5. char[] pwd_char = "pwd1".toCharArray();
6.
7. boolean auth = db.authenticate("user1",pwd_char);//登陆验证,成功之后才能进行有效操作
8.
9. if(!auth){
10. throw new RuntimeException();
11. }

Mongo m = new Mongo();

DB db = m.getDB("admin");

char[] pwd_char = "pwd1".toCharArray();

boolean auth = db.authenticate("user1",pwd_char);//登陆验证,成功之后才能进行有效操作

if(!auth){
throw new RuntimeException();
}






(3)PHP方式通过用户密码访问MongoDB


Php代码 收藏代码

1. //PHP是直接在获取连接对象时就进行配置了
2. //mongodb://账号:密码@连接地址
3.
4. $m = new Mongo("mongodb://user1:pwd1@localhost");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值