mongodb内嵌文档的javaapi,增删改查

时间:2022-07-02 08:45:18

数据结构:

{"_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));