Aggregation写法
History history = null;
//封装对象列表查询条件
List<AggregationOperation> commonOperations = new ArrayList<>();
//1. 指定查询主文档
MatchOperation match = Aggregation.match(Criteria.where("userId").is("2"));
commonOperations.add(match);
//2. 指定投影,返回哪些字段
ProjectionOperation project = Aggregation.project("historyList");
commonOperations.add(project);
//3. 拆分内嵌文档
UnwindOperation unwind = Aggregation.unwind("historyList");
commonOperations.add(unwind);
//4. 指定查询子文档
MatchOperation match2 = Aggregation.match(
Criteria.where("historyList.hh").is("2"));
commonOperations.add(match2);
//创建管道查询对象
Aggregation aggregation = Aggregation.newAggregation(commonOperations);
AggregationResults<JSONObject> reminds = mongoTemplate
.aggregate(aggregation, "history", JSONObject.class);
List<JSONObject> mappedResults = reminds.getMappedResults();
if (mappedResults != null && mappedResults.size() > 0) {
history = JSONObject
.parseObject(mappedResults.get(0).getJSONObject("historyList").toJSONString(), History.class);
mongo格式:{ "_id" : "1", "userId" : "2", "bookId" : "1", "historyList" : [ { "_id" : "1", "hh" : "1" }, { "_id" : "2", ...
db.getCollection('product').find({'coverage':{'$elemMatch':{'plan':{'$elemMatch':{'iscoverage':{'$in':['1']}}}}}})
其中,coverage是文档的第一级子文档,$elemMatch匹配的是数组,然后plan是数组里面的字段,其他一样
转载于:https://www.cnblogs.c...
最近在项目中使用mongdb来保存压测结果中的监控数据,那么在获取监控数据时,遇到这样一个问题: 一个doucument中包含一个内嵌数组,其中内嵌数组也是分成好几类的数组(可以通过标识判断),那么我只需要返回特定的数组,而不是返回内嵌数组的所有数据。
原始数据:
"_id" : ObjectId("5aab3460353df3bd352e0e15"),
Query query = new Query();
query.addCriteria(Criteria.where(单独的字段).is(某个值));
query.fields().elemMatch(子文档所在字段,new Criteria(子文档中某个参数).is(某个值));
mongoTemplate.findOne(query, GatewayDoc.class);
以往一直在程序中写死的mongodb查询collection,对于频繁改动的mongodb来说,每次更改都要进行代码修改,费时费力还有风险,故而希望使用动态配置的方式来进行collection查询。
以往映射关系如下:
由于需要动态配置,首先想到将collection信息存储在zookeeper配置中心里(项目组使用zk做配置中心),所以希望使用@Value的形式来获得,但是由于mongd...
使用 Spring Data MongoDB时,我们可能需要限制从数据库对象映射的属性。通常,出于安全原因,我们可能需要这样做—以避免暴露存储在服务器上的敏感信息。或者,例如,我们可能需要过滤掉 Web 应用程序中显示的部分数据。
在这个简短的教程中,我们将看到 MongoDB 如何应用字段限制。
mongo集合嵌套操作开始1、查询父集合2、更新父集合3、删除/批删父集合(1,2,3)4、新增父集合5、查询子集合5、更新子集合6、删除/批删子集合7、添加子集合结束!
前几天项目需要,集合里面套集合操作,查了不少资料都是看都不看就抄别人的,自己也不验证一下,坑人!!!
操作都不难,但是要费点时间,为了下次不浪费时间,这里记录一下,再遇到可以直接用,好记性不如烂笔头!
数据示例:
"_id" : ObjectId("5fd09ec6ad5b083ads840"),
"type
如果觉得有用记得给个关注
import org.bson.Document;
工作中使用关联查询两个表并返回Document map给前端进行数据展示发现数据结构有问题好多字段为ArrayList类型
相关截图及语句如下:
优化前语句 去除部分敏感字段
MatchOperation matchOperation = Aggregation.match(Criteria.where("username").is(username));
LookupOperati
{ "subject": "数学", "score": 80 },
{ "subject": "语文", "score": 90 },
{ "subject": "英语", "score": 70 }
"_id": 2,
"name": "李四",
"scores": [
{ "subject": "数学", "score": 60 },
{ "subject": "语文", "score": 80 },
{ "subject": "英语", "score": 90 }
"_id": 3,
"name": "王五",
"scores": [
{ "subject": "数学", "score": 90 },
{ "subject": "语文", "score": 60 },
{ "subject": "英语", "score": 80 }
现在,我们想要查询所有在所有科目中都获得了 80 分以上的学生,则可以使用以下查询语句:
db.students.find({
scores: { $elemMatch: { score: { $gte: 80 } } }
这样,查询结果就会包含所有在所有科目中都获得了 80 分以上的学生的信息,即:
"_id": 1,
"name": "张三",
"scores": [
{ "subject": "数学", "score": 80 },
{ "subject": "语文", "score": 90 },
{ "subject": "英语", "score": 70 }
"_id": 2,
"name": "李四",
"scores": [
{ "subject": "数学", "score": 60 },
{ "subject": "语文", "score": 80 },
{ "subject": "英语", "score": 90 }