mongodb使用-增删改查、colleciton关联、_id作为主键实现update
·
题记:最近在公司做的项目,基于SDN的开源项目OpenDaylight实现的,关于OpenDaylight这里就不详细描述,感觉最重要的就是odl的xxx.yang 文件,这个文件就是定义了controller和web的交互的数据结构。这篇主要是讲下mongodb数据库的操作。web前端通过ajax把数据按照yang的格式传给servlet,然后servlet再给相应的url传输,就是对应的yang文件的某个rpc,rpc收到后进行解析。
说明:因为整个工作无法copy,所以就讲几个重点代码文件上传。主要是是实现数据库的操作以及web前端界面的代码,上上篇文章讲的主要是前端的demo,工程下载链接:
http://download.csdn.net/detail/u011563903/8931021
1 数据库关联实现
先把我的前端图片贴出来:主要是分为两个表,device table和group table,device table里面存储的是设备的name,ip,type,group 等,web前端的代码在之后贴出来,第一个table的数据库的内容:
{ "_id" : ObjectId("55b18869e4b052ed8580ebf7"), "name" : "RF-Switch3", "ip" : "192.168.1.4", "type" : "RF-Switch", "community" : "public", "deviceindex" : 6 }
{ "_id" : ObjectId("55b188c7e4b052ed8580ebf8"), "name" : "RF-Switch4", "ip" : "192.168.1.5", "type" : "Ethernet-Switch", "community" : "public", "deviceindex" : 6 }
{ "_id" : ObjectId("55b188c7e4b052ed8580ebf8"), "name" : "RF-Switch4", "ip" : "192.168.1.5", "type" : "Ethernet-Switch", "community" : "public", "deviceindex" : 6 }
并且可以在这里面实现add ,edit,delete等
然后第二个表格主要是存放的group 信息:
> db.grouptable.find()
{ "_id" : ObjectId("55b166e3e4b0e9ab0afd13d3"), "group" : "ScrumTeam0", "index" : 6 }
{ "_id" : ObjectId("55b18814e4b052ed8580ebf6"), "group" : "ScrumTeam1", "index" : 7 }
{ "_id" : ObjectId("55b166e3e4b0e9ab0afd13d3"), "group" : "ScrumTeam0", "index" : 6 }
{ "_id" : ObjectId("55b18814e4b052ed8580ebf6"), "group" : "ScrumTeam1", "index" : 7 }
为了在group table表格中可以通过edit的时候,同样把device table中的也改掉,必须要做数据库关联。我采用的是人工关联
数据库关联的实现:
在ta
2 _id作为主键实现
之前做的时候因为把ip地址当做了主键,但是现在是ip地址也是可以编辑的所有要把自动生成的_id作为主键,因为这个不是自增的所以不能用来进行关联数据库,只能通过上面的方法去做人工关联,重点_id转成String方法,具体可以去看它的API.具体_id的convert在get和edit函数中做了实现。
3 add数据
我这个add函数中,是先去查找
public String addDevice(String name, String ip, String type, String community,String groupname) {
// TODO Auto-generated method stub
//must groupname index query
DBObject query2 = new BasicDBObject();
query2.put("group", groupname);//check ip repeat
DBObject result2 = collectionGroup.findOne(query2);
int index2 = (int) result2.get("index");
DBObject query = new BasicDBObject();
query.put("ip", ip);//check ip repeat
List<DBObject> result = collectionDevice.find(query).toArray();
if(null != result && result.size() > 0){
return "sorry,database have this ip";
}
else{
DBObject query1 = new BasicDBObject();
query1.put("name", name);
query1.put("ip", ip);
query1.put("type", type);
query1.put("community", community);
query1.put("deviceindex", index2);
collectionDevice.insert(query1);
}
return "success";
}
4 delete数据
delete数据比较简单,主要是有个查询数据的看看是否存在。
public String removeDevice(String name, String ip, String type,String community,int index) {//three paramters must have in database,so can remove
// TODO Auto-generated method stub
BasicDBObject query = new BasicDBObject();
query.put("name", name);
query.put("ip", ip);
query.put("type", type);
query.put("community", community);
query.put("deviceindex", index);
List<DBObject> result = collectionDevice.find(query).toArray();
if(null == result || result.size() <= 0){
return "failed";
}
if(null != result && result.size() > 0){
collectionDevice.remove(query);
}
return "success";
}
5 get数据
要从根据device的index去查grouptable中对应的index的group 名字是什么,所有有个for循环遍历,先将所有的数据得到放到list里面,然后得到每一个DBobject对象,然后把{ "deviceid" : { "$oid" : "55b01e08e4b04009dcbca85c"}变成{ "_id" : "55b01e08e4b04009dcbca85c" }这种类型,然后去得到index,再用这个index去查对应的name,再put到原来的list里面,具体是吸纳如下。
@Override
public String getDevice(String name, String ip, String type,String community,int index) {
// TODO Auto-generated method stub
List<DBObject> result=new ArrayList<DBObject>();
result = collectionDevice.find().toArray();
for(int i=0;i<result.size();i++){
//put "_id":{} to "_id":""
DBObject dbodevice = result.get(i);//get one document
Object aa = dbodevice.get("_id");
dbodevice.put("_id", aa.toString());
Object indexdbo = dbodevice.get("deviceindex");//get deviceindex of one document
DBObject groupresult = new BasicDBObject();
groupresult.put("index", indexdbo);
DBObject dbogroup = collectionGroup.findOne(groupresult);
dbodevice.put("devicegroup",dbogroup.get("group").toString());
}
String resultstring = result.toString();
System.out.println("db:"+ resultstring);
return resultstring;
}
@Override
public String getDevice(String name, String ip, String type,String community,int index) {
// TODO Auto-generated method stub
List<DBObject> result=new ArrayList<DBObject>();
result = collectionDevice.find().toArray();
for(int i=0;i<result.size();i++){
//put "_id":{} to "_id":""
DBObject dbodevice = result.get(i);//get one document
Object aa = dbodevice.get("_id");
dbodevice.put("_id", aa.toString());
Object indexdbo = dbodevice.get("deviceindex");//get deviceindex of one document
DBObject groupresult = new BasicDBObject();
groupresult.put("index", indexdbo);
DBObject dbogroup = collectionGroup.findOne(groupresult);
dbodevice.put("devicegroup",dbogroup.get("group").toString());
}
String resultstring = result.toString();
System.out.println("db:"+ resultstring);
return resultstring;
}
6 update数据
这部分是这几个里面最麻烦的一个方法了,这个要仔细看,因为涉及到的一个问题是,我在edit的时候,因为ip地址是唯一的,所有当用户改变ip地址的时候,那么经过find发现没有数据,则说明此ip地址唯一,直接update就可以,但是如果用户没有改变ip地址呢,那么查找的找到这个ip地址的数据,这个时候就需要根据_id去判断,是否存在ip地址重复。详细请看下面代码:
@Override
public String editDevice(String name, String ip, String type,String community,String groupname,String id_device) {//update
// TODO Auto-generated method stub
//0 convert groupname to device_index
DBObject queryindex = new BasicDBObject();
queryindex.put("group", groupname);//check groupname repeat
DBObject resultindex = collectionGroup.findOne(queryindex);
int index0 = (int) resultindex.get("index");
//1 query have ip?
DBObject queryip = new BasicDBObject();
queryip.put("ip", ip);
List<DBObject> resultip = collectionDevice.find(queryip).toArray();
if(resultip == null || resultip.size()<=0){
//2 not have the ip,update
@SuppressWarnings("deprecation")
ObjectId iddevice = ObjectId.massageToObjectId(id_device);
DBObject queryupdate = new BasicDBObject();
queryupdate.put("_id",iddevice);
queryupdate.put("name", name);
queryupdate.put("ip", ip);
queryupdate.put("type", type);
queryupdate.put("community", community);
queryupdate.put("deviceindex", index0);
collectionDevice.update(new BasicDBObject().append("_id", iddevice), queryupdate);
return "success";
}
{
//3 have this ip
//3.1 old ip,check this ip -- _id =? id_device
DBObject query_id = new BasicDBObject();
query_id.put("ip", ip);
DBObject ip_id = collectionDevice.findOne(query_id);
String ip_id_str = ip_id.get("_id").toString();
if(ip_id_str.equalsIgnoreCase(id_device)){
//3.1.1 if equals,update
@SuppressWarnings("deprecation")
ObjectId iddevice = ObjectId.massageToObjectId(id_device);
DBObject queryupdate = new BasicDBObject();
queryupdate.put("_id",iddevice);
queryupdate.put("name", name);
queryupdate.put("ip", ip);
queryupdate.put("type", type);
queryupdate.put("community", community);
queryupdate.put("deviceindex", index0);
collectionDevice.update(new BasicDBObject().append("_id", iddevice), queryupdate);
return "success";
}
else{
//3.1.2 not equals ,can not update
return "sorry,database have this ip";
}
}
}
更多推荐
已为社区贡献4条内容
所有评论(0)