mongodb內嵌文檔的javaapi,增刪改查


數據結構:

{"_id" : "000000001",  //Mongodb默認主鍵

 "UID" : "000000001",  //SVC UID

 "CREATE_DATE" : "2016-10-21 00:00:00",  //創建時間

 "OP_DATE" : "2016-10-21 00:00:00",  //修改時間

 "BASE_TAG_LIST" :  //基礎標簽列表

 [

       { "TAG_ID" : "A01", "TAG_CODE" : "1", "TAG_VALUE" : "1" },  //詳細標簽,此處為內嵌文檔

       { "TAG_ID" : "A02", "TAG_CODE" : "0", "TAG_VALUE" : "0" },

       { "TAG_ID" : "A03", "TAG_CODE" : "000000", "TAG_VALUE" : "000000" }

 ]}

內嵌文檔創建符合索引:

MongoCollection<Document> collection = db.getCollection(table);
Document docIndex = new Document();
for (int index = 0;index<list.size();index++){
String indexPara = (String)list.get(index);
String[] indexParaList = indexPara.split("\\+");
System.out.println(indexParaList[0]);//key
System.out.println(indexParaList[1]);//+-1
//組合復合索引
docIndex.append(indexParaList[0],Integer.parseInt(indexParaList[1]));
}
collection.createIndex(docIndex);

普通查詢:

命令:db.tag.find({ "UID": "000000001" )

java:
BasicDBObject doc = new BasicDBObject();
doc.put("UID",uid);
MongoCollection<Document> collection = db.getCollection(table);
FindIterable<Document> iterable = collection.find(doc);
/**
* 1. 獲取迭代器FindIterable<Document> 2. 獲取游標MongoCursor<Document>
* 3.通過游標遍歷檢索出的文檔集合
* */

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
MongoCursor<Document> cursor = iterable.iterator();
while (cursor.hasNext()) {
Document user = cursor.next();
}
 
        
內嵌文檔查詢:
命令:

db.tag.find({"BASE_TAG_LIST.": { $all: [{$elemMatch: {TAG_ID: "A01", TAG_VALUE: "0"}}, {$elemMatch: {TAG_ID: "A02", TAG_VALUE: "1"}}] }})

java:

List dBObjectElelist = new ArrayList<BasicDBObject>();
for (Map.Entry<String, String> entry : map.entrySet()) {
BasicDBObject doc1 = new BasicDBObject();
doc1.put("TAG_ID",entry.getKey());
doc1.put("TAG_VALUE",entry.getValue());
BasicDBObject EleDoc = new BasicDBObject("BASE_TAG_LIST",new BasicDBObject("$elemMatch", doc1));
dBObjectElelist.add(EleDoc);
}

BasicDBObject queryObject = new BasicDBObject()
.append(QueryOperators.AND,dBObjectElelist );
FindIterable<Document> cursor = db.getCollection("bdp_user_tag").find(queryObject);

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Document d: cursor) {
String jsonString = d.toJson();
Map<String, Object> jsonStrToMap = JsonStrToMap
.jsonStrToMap(jsonString);
list.add(jsonStrToMap);
}
return list;

內嵌文檔的刪除和添加修改:
需求:

修改用戶的標簽。比如,用戶的標簽是A01, 標簽值是0。

實現場景1:修改標簽值

實現場景2:刪除標簽A01

實現場景3:添加標簽A05

修改:

//查找到uid,基礎標簽tagvalue所在的文檔 oldDoc ,得到docUpdate文檔
Document oldDoc= new Document("UID",uid);
Document docUpdate= baseIterable.first();//Document d = collection.find(oldDoc).first();
//由文檔得到內嵌文檔的key,得到內嵌文檔的value數組:這時可以對內嵌文檔進行數組型的修改
ArrayList<Document> arr=(ArrayList<Document>)(docUpdate.get("BASE_TAG_LIST"));
Integer oldtagIndex = null;
for (int index =0;index < arr.size();index++){
Document docEle = arr.get(index);
if(docEle.get("TAG_ID").equals(tagid) && docEle.get("TAG_VALUE").equals(oldTagvalue)){
oldtagIndex = index;
}
}
if(null != oldtagIndex){
Document arrEleUpdate = arr.get(oldtagIndex);
arrEleUpdate.remove("TAG_VALUE");
arrEleUpdate.put("TAG_VALUE",newTagvalue);
Document newDoc = new Document("BASE_TAG_LIST",arr);
//更新操作,本質是取出內嵌文檔的數組,進行修改后更新
collection.updateOne(oldDoc, new Document("$set",newDoc));
}

內嵌文檔的添加和刪除:
是對上文中arr數組內元素的刪除和添加:
刪除:arr.remove(int變量);
添加:arr.add(new Document().append(key,value));
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM