java操作mongodb数据库
mongoDB的java驱动
1、DBObject对象
如:{name:"suns",pwd:"123"}---------->Map.put("name","suns")----->DBObject是Map的子接口
-------------->BasicDBObject.put("name","suns");
2、BasicDBList对象(数组的形式)
["suns","jiangxs","huxz"]------>ArrayList---------->BasicDBList.add("suns")子类
3、java编程步骤
a、导入MongDB提供的java驱动jar包
b、创建链接对象----------》负责与数据库的链接
Mongo mongo=new Mongo(ip,port);
c、获得数据库对象
DB db=mongo.getDB("dbname");
d、获得Collection对象
DBCollection dbCollection=db.getCollection("collectionName");
e、调用DBCollection对象的相关方法完成MongoDb的CRUD操作
(1)dbCollection.insert()------------->db.insert();
(2)dbCollection.find() ------------->db.users.find();
(3)dbCollection.findOne()----------->db.users.findOne();
(4)dbCollection.update()------------>db.users.update();
(5)dbCollection.remove()------------>db.users.remove();
4、启动Eclipse演示代码
public class TestMongoAPI{
private Mongo mongo;
private DB db;
private DBCollection dbCollection;
@Before//初始化方法
public void init()throws Exception{
mongo=new Mongo("127.0.0.1","27017");
db=mongo.getDB("baichi");
dbCollection=db.getCollection("users");
}
/**1、测试插入对象
*db.users.insert(name:"suns",password:"123456");
*db.Collection.insert()
*/
@Test
public void testInsert(){
//新建DB对象
DBObject dbObject=new BasicDBObject();
dbObject.put("name","suns");
dbObject.put("password","123456");
//将值插入数据库中
dbCollection.insert(dbObject);
}
/**
*执行插入java代码后,检查是否插入MongoDB数据库
*>db----->>use baichi----->db.users.find()
*/
/**
*2、测试插入集合
*db.users.insert({post,[1,2,3]})
* dbCollection.insert()
*/
@Test
public void testListInsert(){
BasicDBList dbList=new BasicDBList();
dbList.add(1);
dbList.add(2);
dbList.add(3);
//创建对象准备放入集合
DBObject dbObject=new BasicDBObject();
dbObject.put("post",dbList);
dbCollection.insert(dbObject);
}
/**
*3、测试批量数据的插入
*{name:"suns1",password:"123456"}
*{name:"suns2",password:"123456"}
*{name:"suns3",password:"123456"}
*/
@Test
public void testBatchIbsert(){
//创建3个对象,准备存储数据
DBObject dbObject1=new BasicDBObject();
DBObject dbObject2=new BasicDBObject();
DBObject dbObject3=new BasicDBObject();
//放入数据到map集合中
dbObject1.put("name","suns1");
dbObject1.put("password","123456");
dbObject1.put("name","suns2");
dbObject1.put("password","123456");
dbObject1.put("name","suns3");
dbObject1.put("password","123456");
List<DBObject> dbObjects=new ArrayList<DBObject>();
dbObjects.add(dbObject1);
dbObjects.add(dbObject2);
dbObjects.add(dbObject3);
//重载的方法
dbCollection.insert(dbObjects);
}
/**
*4、测试数据的查询(一)
* db.users.find({name:"suns"});
* dbCollection.find()
*/
@Test
public void testQuery(){
DBObject dbObject=new BasicDBObject();
dbObject.put("name","suns");
DBCursor dbCursor=dbCollection.find(dbObject);//添加查询条件dbObject
//遍历查询结果
while(dbCursor.hasNext()){
System.out.println(dbCursor.next());
}
}
/**
*5、测试数据的查询(二)
*db.users.find({name:{$ne:"suns"}});
* dbCollection.find();
*/
@Test
public void testQuery2(){
//新建需要封装的对象
DBObject neObject=new BasicDBObject();
neObject.put("$ne","suns");
DBObject nameObject=new BasicDBObject();
nameObject.put("name",neObject);
//调用对象的方法,执行语句
DBCursor dbcursor=dbCollection.find(nameObject);
while(dbcursor.hashNext()){
System.out.println("结果:"+dbcursor.next());
}
}
/**
*6、测试修改数据
*db.users.update({name:"suns"},{$set:{password:"23564"}},0,1);
*0位置代表如果不存在,是否插入(0否,1是);1位置代表是否影响多行(0否,1是)
* dbCollection.update()
*/
@Test
public void testUpdate(){
DBObject queryObject=new BasicDBObject();
queryObject.put("name","suns");
DBObject setCondition=new BasicDBObject();
setCondition.put("password","23564");
DBObject setObject=new BasicDBObject();
setObject.put("$set",setCondition);
//调用对象的方法,执行修改
dbCollection.update(queryObject,setObject,false,true);
}
/**
*7、测试删除数据
*db.users.remove({name:"suns"});
* dbCollection.remove();
*/
@Test
public void testDelete(){
//准备封装参数
DBObject removeCondition=new BasicDBObject();
removeCondition.put("name","suns");
//调用对象方法,删除数据
dbCollection.remove(removeCondition);
}
}