数据创建
db.arrayquery.insert([
{
"_id" : NumberLong(3),
"name" : "n1",
"creator" : "c3",
"contentList" : [
{
"name" : "皇冠梨3",
"hover" : 20,
"content" : {
"source" : "s1",
"mId" : "m0001",
"name" : "皇冠梨",
"type" : 2
}
},
{
"name" : "模板2",
"hover" : 10,
"content" : {
"source" : "s1",
"mId" : "m0001",
"name" : "模板2",
"type" : 2
}
},
{
"name" : "10寸西红柿",
"hover" : 13,
"content" : {
"source" : "s2",
"mId" : "m0001",
"name" : "10寸西红柿",
"type" : 2
}
}
],
"gmtCreate" : NumberLong("2718064537"),
"gmtModified" : NumberLong("2718064537"),
"serial" : 1,
"priority" : 20
},
{
"_id" : NumberLong(2),
"name" : "n1",
"creator" : "c2",
"contentList" : [
{
"name" : "皇冠梨",
"hover" : 20,
"content" : {
"source" : "s1",
"mId" : "m0001",
"name" : "皇冠梨",
"type" : 2
}
},
{
"name" : "模板2",
"hover" : 10,
"content" : {
"source" : "s1",
"mId" : "m0001",
"name" : "模板2",
"type" : 2
}
},
{
"name" : "10寸西红柿",
"hover" : 13,
"content" : {
"source" : "s2",
"mId" : "m0001",
"name" : "10寸西红柿",
"type" : 2
}
}
],
"gmtCreate" : NumberLong("2718064537"),
"gmtModified" : NumberLong("2718064537"),
"serial" : 1,
"priority" : 20
},
{
"_id" : NumberLong(1),
"name" : "n1",
"creator" : "c1",
"contentList" : [
{
"name" : "皇冠梨",
"hover" : 20,
"content" : {
"source" : "s1",
"mId" : "m0001",
"name" : "皇冠梨",
"type" : 2
}
},
{
"name" : "模板2",
"hover" : 10,
"content" : {
"source" : "s1",
"mId" : "m0001",
"name" : "模板2",
"type" : 2
}
},
{
"name" : "10寸西红柿",
"hover" : 13,
"content" : {
"source" : "s2",
"mId" : "m0001",
"name" : "10寸西红柿",
"type" : 2
}
}
],
"gmtCreate" : NumberLong("2718064537"),
"gmtModified" : NumberLong("2718064537"),
"serial" : 1,
"priority" : 20
}]);
数据查询
db.arrayquery.aggregate(
[
{
$match : { "contentList.name": "皇冠梨" }
},
{
$project: {
"customer": 1,
"name": 1,
"contentList": {
$filter: {
input: "$contentList",
as: "item",
cond: { $eq : ["$$item.name","皇冠梨"] }
}
}
}
},{
$sort:{"name":-1
}
},{
$limit:3
},{$skip:0}
]
)
代码实现
@RequestMapping("/test")
@ResponseBody
public Object test() throws Exception {
List<Document> aggregateList = new ArrayList<>();
//拼接条件
Document sub_match = new Document();
Document match = new Document("$match", sub_match);
sub_match.append("contentList.name", "皇冠梨");
aggregateList.add(match);
//显示数据
Document sub_project = new Document();
sub_project.append("customer", 1);
sub_project.append("name", 1);
sub_project.append("contentList"
, new Document("$filter",
new Document()
.append("input", "$contentList")
.append("as", "item")
.append("cond", new Document("$eq", Arrays.asList("$$item.name", "皇冠梨"))))
);
Document project = new Document("$project", sub_project);
aggregateList.add(project);
Document sort_project = new Document("name", -1);
Document sort = new Document("$sort", sort_project);
aggregateList.add(sort);
aggregateList.add(new Document("$limit",1));
aggregateList.add(new Document("$skip",0));
AggregateIterable aggregateIterable = mongoTemplate.getCollection("arrayquery").aggregate(aggregateList).allowDiskUse(true);
MongoCursor cursor = aggregateIterable.iterator();
List<Object> list = new ArrayList<>();
while (cursor.hasNext()) {
Object object = cursor.next();
list.add(object);
System.out.println(JSON.toJSONString(object));
}
return list;
}
#############################start $and $or 嵌套#################################
// db.getCollection('marketingScheduler').aggregate(
// [
// {
// $match : { $or:[
// {$and:[{"schedulerContentList.name": "皇冠梨" },{"customer":"9527"}]},
// {$and:[{"name":"AA"},{"customer" : "9527"}]}
// ]
// }
// },
// {
// $project: {
// "customer": 1,
// "name": 1,
// "schedulerContentList": {
// $filter: {
// input: "$schedulerContentList",
// as: "item",
// cond: { $eq : ["$$item.name","皇冠梨"] }
// }
// }
// }
// },{
// $sort:{"name":-1
// }
// },{
// $limit:20
// },{$skip:0}
//
// ]
//)
List<Document> aggregateList = new ArrayList<>();
//拼接条件
// Document sub_match = new Document();
List<Document> match_or_list= new ArrayList<>();
Document match_or = new Document("$or",match_or_list);
Document match = new Document("$match", match_or);
List<Document> match_or_list_and1_list= new ArrayList<>();
Document match_or_list_and1 = new Document("$and", match_or_list_and1_list);
match_or_list_and1_list.add(new Document().append("schedulerContentList.name","皇冠梨" ).append("customer","9527"));
List<Document> match_or_list_and2_list= new ArrayList<>();
Document match_or_list_and2 = new Document("$and", match_or_list_and2_list);
match_or_list_and2_list.add(new Document().append("name","AA" ).append("customer","9527"));
match_or_list.add(match_or_list_and1);
match_or_list.add(match_or_list_and2);
aggregateList.add(match);
//显示数据
Document sub_project = new Document();
sub_project.append("customer", 1);
sub_project.append("name", 1);
// sub_project.append("contentList"
// , new Document("$filter",
// new Document()
// .append("input", "$contentList")
// .append("as", "item")
// .append("cond", new Document("$eq", Arrays.asList("$$item.name", "皇冠梨"))))
//
// );7
Document project = new Document("$project", sub_project);
aggregateList.add(project);
Document sort_project = new Document("name", -1);
Document sort = new Document("$sort", sort_project);
aggregateList.add(sort);
aggregateList.add(new Document("$limit",10));
aggregateList.add(new Document("$skip",0));
AggregateIterable aggregateIterable = mongoTemplate.getCollection("marketingScheduler").aggregate(aggregateList).allowDiskUse(true);
MongoCursor cursor = aggregateIterable.iterator();
List<Object> list = new ArrayList<>();
while (cursor.hasNext()) {
Object object = cursor.next();
list.add(object);
System.out.println(JSON.toJSONString(object));
}
return list;
#############################end $and $or 嵌套#################################
MongoDB 数组查询(数组中符合条件返回)+分页+排序
最新推荐文章于 2024-07-12 10:09:52 发布