mongodb java 内嵌文档_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 collection = db.getCollection(table);

Document docIndex = new Document();

for (int index = 0;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 collection = db.getCollection(table);

FindIterable iterable = collection.find(doc);

/**

* 1. 获取迭代器FindIterable 2. 获取游标MongoCursor

* 3.通过游标遍历检索出的文档集合

* */

List> list = new ArrayList>();

MongoCursor 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();

for (Map.Entry 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 cursor = db.getCollection("bdp_user_tag").find(queryObject);

List> list = new ArrayList>();

for (Document d: cursor) {

String jsonString = d.toJson();

Map 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 arr=(ArrayList)(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));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值