MongoDB 数组查询(数组中符合条件返回)+分页+排序
数据创建
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;
}