1、查询返回单个文档JSON数据
(1)查询返回独立文档
MongoDB语法:
.find({"ID":"2015040011"})
Soap请求:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"><SOAP:Body>
<FindDocuments xmlns="http://schemas.cordys.com/XiaoywWSAppServerPackage" preserveSpace="no" qAccess="0" qValues="">
<collectionName>BusinessEntity</collectionName>
<querycondition>{"ID":"2015040011"}</querycondition>
</FindDocuments>
</SOAP:Body>
</SOAP:Envelope>
返回值截取内容如下:
<findDocuments>
{ "_id" : { "$oid" : "5525d9e084ae4890fc58c489"} , "ID" : "2015040011" , "Name" : "高级采购流程" , "Dept" : "经营管理部"}</findDocuments>
(2)查询返回嵌套文档
MongoDB语法:
.find({"ID":"2015040013"})
Soap请求:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<FindDocuments xmlns="http://schemas.cordys.com/XiaoywWSAppServerPackage" preserveSpace="no" qAccess="0" qValues="">
<collectionName>BusinessEntity</collectionName>
<querycondition>{"ID":"2015040013"}</querycondition>
</FindDocuments>
</SOAP:Body>
</SOAP:Envelope>
嵌套文档查询结果截取主要内容如下:
<findDocuments>
{ "_id" : { "$oid" : "55260ce084ae4cb2fa2e2bea"} , "ID" : "2015040013" , "Name" : "新版合作伙伴采购流程" , "Dept" : "经营管理部" , "DETAIL" :
[ { "WF" : "01" , "WFNAME" : "启动"} , { "WF" : "02" , "WFNAME" : "结束"}]}
</findDocuments>
2、扩展/修改文档,返回文档_id
(1)插入单文档操作
MongoDB语法:
.save({"ID":"2015040012","Name":"新版外包采购流程","Dept":"经营管理部"})
Soap请求:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<GetExpandDocument xmlns="http://schemas.cordys.com/XiaoywWSAppServerPackage" preserveSpace="no" qAccess="0" qValues="">
<collectionName>BusinessEntity</collectionName>
<JSONString>{"ID":"2015040012","Name":"新版外包采购流程","Dept":"经营管理部"}</JSONString>
</GetExpandDocument>
</SOAP:Body>
</SOAP:Envelope>
返回_id值如下:
<getExpandDocument>5525e34d84ae4cb2fa2e2be6</getExpandDocument>
(2)插入嵌套文档(相当于一对多关系)
MongoDB语法:
.save({ID:'2015040013',Name:'新版合作伙伴采购流程',Dept:'经营管理部',DETAIL:[{WF:'01',WFNAME:'启动'},{WF:'02',WFNAME:'结束'}]})
Soap请求:
<GetExpandDocument xmlns="http://schemas.cordys.com/XiaoywWSAppServerPackage" preserveSpace="no" qAccess="0" qValues="">
<collectionName>BusinessEntity</collectionName>
<JSONString>{ID:'2015040013',Name:'新版合作伙伴采购流程',Dept:'经营管理部',DETAIL:[{WF:'01',WFNAME:'启动'},{WF:'02',WFNAME:'结束'}]}</JSONString>
</GetExpandDocument>
(3)修改数据,在_id值不变的情况下,执行修改操作
MongoDB语法:
.save({"_id":{"$oid":"5525d9e084ae4890fc58c489"},"ID":"2015040011","Name":"经领导外包采购流程","Dept":"经营管理部"})
Soap请求:
<GetExpandDocument xmlns="http://schemas.cordys.com/XiaoywWSAppServerPackage" preserveSpace="no" qAccess="0" qValues="">
<collectionName>BusinessEntity</collectionName>
<JSONString>{"_id":{"$oid":"5525d9e084ae4890fc58c489"},"ID":"2015040011","Name":"经领导外包采购流程","Dept":"经营管理部"}
</JSONString>
</GetExpandDocument>
附件代码:
/*
This class has been generated by the Code Generator
*/
package com.unicom.xiaoywwsappserverpackage;
import com.cordys.cpc.bsf.busobject.BusObjectConfig;
import com.cordys.cpc.bsf.busobject.BusObjectIterator;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.WriteResult;
import com.mongodb.util.JSON;
public class C_MongoPackage extends C_MongoPackageBase
{
public C_MongoPackage()
{
this((BusObjectConfig)null);
}
public C_MongoPackage(BusObjectConfig config)
{
super(config);
}
/* 内部私有共享方法:用于链接数据库,需要传输DB */
private static DB connectDB(DB v_db){
try {
MongoClientURI uri = new MongoClientURI("mongodb://用户名:密码@10.64.52.120:27017/数据库名称");
MongoClient client = new MongoClient(uri);
v_db = client.getDB("ayuming");
} catch (Exception e) {
e.printStackTrace();
}
return v_db;
}
/* 查询返回单个文档,输入参数分别为Collection名称、查询条件 */
public static String findDocuments(String collectionName, String querycondition)
{
DB db = null;
String ret_str = null;
try {
db = connectDB(db);
DBCollection collection = db.getCollection(collectionName);
DBObject query = (BasicDBObject) JSON.parse(querycondition);
DBCursor dbCursor = collection.find(query);
int curCount = dbCursor.count();
if (curCount > 0){
ret_str = dbCursor.next().toString();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
try {
db.requestDone();
db = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return ret_str;
}
public static BusObjectIterator<com.unicom.xiaoywwsappserverpackage.C_MongoPackage> getC_MongoPackageObjects(com.cordys.cpc.bsf.query.Cursor cursor)
{
// TODO implement body
return null;
}
/* 保存单个文档到数据库中,输入参数为Collection名称、JSON格式的文档,支持嵌套模型 */
public static String getExpandDocument(String collectionName,String JSONString) {
DB db = null;
String ret_id = null;
try {
db = connectDB(db);
DBCollection collection = db.getCollection(collectionName);
DBObject query = (BasicDBObject) JSON.parse(JSONString);
WriteResult writeResult = collection.save(query);
ret_id = query.get("_id").toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (db != null) {
try {
db.requestDone();
db = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return ret_id;
}
public void onInsert()
{
}
public void onUpdate()
{
}
public void onDelete()
{
}
}
注:此代码只是PoC使用,请完善加入日志处理等必要功能再使用到生产环境上。
参考及相关:
《云计算统一办公运营平台服务能力设计方案》 肖永威 2014.11
《用MongoDB数据库来管理办公系统中文档型的表单和信息——通用流程化应用审批单设计思路(二,续)》 肖永威 2015.01