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));
mongodb内嵌文档的javaapi,增删改查的更多相关文章
- MongoDB 内嵌文档
MongoDB是文档型的数据库系统,doc是MongoDB的数据单位,每个doc相当于关系型数据库的数据行(row),doc和row的区别在于field的原子性:row中的column是不和分割的原子 ...
- mongodb内嵌文档的查询
本文转自:http://blog.163.com/wm_at163/blog/static/1321734902012526103825481/ 1 > db.blog.findOne() { ...
- MongoDB内嵌文档操作
实体定义: [BsonIgnoreExtraElements] public class Person : BaseEntity { public string FirstName { get; se ...
- MongoDB学习笔记——文档操作之增删改
插入文档 使用db.COLLECTION_NAME.insert() 或 db.COLLECTION_NAME.save() 方法向集合中插入文档 db.users.insert( { user_id ...
- 多测师讲解接口 _需求文档(用户增删改查)_高级讲师肖sir
首先连接Duoceshi_new网络 密码为Duoceshi_new,因为接口项目部署在Duoceshi_new网段中. 测试工具:postman域名:http://192.168.1.2:8081/ ...
- mongodb对数组元素及内嵌文档进行增删改查操作(转)
from:https://my.oschina.net/132722/blog/168274 比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: &l ...
- MongoDB对数组元素及内嵌文档进行增删改查操作
比如我有一个user类,他包含一个标签属性,这个标签是一个数组,数组里面的元素是内嵌文档,格式如下: { "_id" : "195861", &qu ...
- mongodb查询内嵌文档
mongodb查询内嵌文档 假设有这样一个文档: db.XXX.remove(); db.XXX.insert({"id":1, "members":[{& ...
- MongoDB查询或修改内嵌文档
作为非关系型数据库中的佼佼者,MongoDB一大优势在于能够在一条文档中存储对象类型的数据,适当增加冗余来让数据库更好用.文档中一个对象类型的字段在MongoDB中被称为内嵌文档(Embedded) ...
随机推荐
- django APPEND_SLASH
#设置项是否开启URL访问地址后面不为/跳转至带有/的路径APPEND_SLASH=True
- mysql 线程操作
show processlist;//查询mysql所有连接线程 SELECT CONCAT('KILL ',id,';') FROM information_schema.`PROCESSLIST` ...
- 数据库sql使用小结
1.更新数据库中表的字段的时候,如果更新的表本身含有最细粒度的字段,那么可以以最细粒度作为判断条件,一次性更新多个字段: 2.如果更新的表中本身不含有最细粒度字段,那么要更新多个字段时有两种思路: a ...
- python深拷贝,浅拷贝
1.浅拷贝, 首先要引用copy包 from copy import copy class A: def __init__(self): self.A=1 self.B=2 a = A() copy( ...
- echarts 设置数据加载遮罩层
//显示加载数据的loading chart.showLoading({ text: "图表数据正在努力加载...", x ...
- yarn安装及node升级
ERROR: root@debian:/home/test/keygen-radio-master/scripts# npm install -g yarn npm WARN engine yarn@ ...
- Linux seq_printf输出内容不完整的问题
Linux seq_printf输出内容不完整的问题 写在前面的话:这是多年前在项目中遇到的问题,作为博客的开篇之作,有不足之处,请各位大侠斧正!谢谢! seq_file接口介绍 有许多种方法能够实现 ...
- Mysql表结构导出excel(含数据类型、字段备注注释)
SELECT "列名", "数据类型", "字段类型", "长度", "是否为空&quo ...
- 解题(LeatestCarFee -计算最少过路费)
NowCoder今年买了一辆新车,他决定自己开车回家过年.回家过程中要经过ň个大小收费站,每个收费站的费用不同,你能帮他计算一下最少需要给多少过路费吗? 输入描述: 输入包含多组数据,每组数据第一行包 ...
- [Solution] 893. Groups of Special-Equivalent Strings
Difficulty: Easy Problem You are given an array A of strings. Two strings S and T are special-equiva ...