Java 如何从mongo中找到选定的数组元素
我只想从mongo文档中查找select数组元素。就像在下面的文档中一样,我只想返回数组中的疑难问题。 我正在使用mongo驱动程序Java 如何从mongo中找到选定的数组元素,java,mongodb,mongo-java,Java,Mongodb,Mongo Java,我只想从mongo文档中查找select数组元素。就像在下面的文档中一样,我只想返回数组中的疑难问题。 我正在使用mongo驱动程序 BasicDBObject query=new BasicDBObject("questionList.type", "hard"); DBCursor curssc = collection.find(query); 文件 { "testpaperid": 1, "testpaperNo": "science", "questionList
BasicDBObject query=new BasicDBObject("questionList.type", "hard");
DBCursor curssc = collection.find(query);
文件
{
"testpaperid": 1,
"testpaperNo": "science",
"questionList":
[
{
"question": "this is question no 1",
"type": "hard"
},
{
"question": "this is question no 2",
"type":"simple"
},
{
"question": "this is question no 3",
"type": "hard"
}
]
}
在MongoDB的常规查询中使用时,它将返回与查询条件匹配的文档,并返回整个文档,包括数组字段的内容。这意味着您无法使用常规查询从数组字段中筛选出某些文档
但是您可以使用。以下查询是mongo shell的聚合查询,您可以轻松地将其转换为Java驱动程序代码:
db.quest.aggregate(
[
{$unwind: '$questionList'},
{$match: {'questionList.type': 'hard'}},
{$group:
{
_id: {
_id: '$_id',
testpaperid: '$testpaperid',
testpaperNo: '$testpaperNo'
},
questionList: {$push: '$questionList'}
}
},
{$project: {
_id: '$_id._id',
testpaperid: '$_id.testpaperid',
testpaperNo: '$_id.testpaperNo',
questionList: 1
}
}
]
)
在MongoDB的常规查询中使用时,它将返回与查询条件匹配的文档,并返回整个文档,包括数组字段的内容。这意味着您无法使用常规查询从数组字段中筛选出某些文档
但是您可以使用。以下查询是mongo shell的聚合查询,您可以轻松地将其转换为Java驱动程序代码:
db.quest.aggregate(
[
{$unwind: '$questionList'},
{$match: {'questionList.type': 'hard'}},
{$group:
{
_id: {
_id: '$_id',
testpaperid: '$testpaperid',
testpaperNo: '$testpaperNo'
},
questionList: {$push: '$questionList'}
}
},
{$project: {
_id: '$_id._id',
testpaperid: '$_id.testpaperid',
testpaperNo: '$_id.testpaperNo',
questionList: 1
}
}
]
)
在MongoDB的常规查询中使用时,它将返回与查询条件匹配的文档,并返回整个文档,包括数组字段的内容。这意味着您无法使用常规查询从数组字段中筛选出某些文档
但是您可以使用。以下查询是mongo shell的聚合查询,您可以轻松地将其转换为Java驱动程序代码:
db.quest.aggregate(
[
{$unwind: '$questionList'},
{$match: {'questionList.type': 'hard'}},
{$group:
{
_id: {
_id: '$_id',
testpaperid: '$testpaperid',
testpaperNo: '$testpaperNo'
},
questionList: {$push: '$questionList'}
}
},
{$project: {
_id: '$_id._id',
testpaperid: '$_id.testpaperid',
testpaperNo: '$_id.testpaperNo',
questionList: 1
}
}
]
)
在MongoDB的常规查询中使用时,它将返回与查询条件匹配的文档,并返回整个文档,包括数组字段的内容。这意味着您无法使用常规查询从数组字段中筛选出某些文档
但是您可以使用。以下查询是mongo shell的聚合查询,您可以轻松地将其转换为Java驱动程序代码:
db.quest.aggregate(
[
{$unwind: '$questionList'},
{$match: {'questionList.type': 'hard'}},
{$group:
{
_id: {
_id: '$_id',
testpaperid: '$testpaperid',
testpaperNo: '$testpaperNo'
},
questionList: {$push: '$questionList'}
}
},
{$project: {
_id: '$_id._id',
testpaperid: '$_id.testpaperid',
testpaperNo: '$_id.testpaperNo',
questionList: 1
}
}
]
)
如果使用运算符,则会限制和mongo聚合查询的内容,如下所示:
db.collectionName.aggregate({
"$unwind": "$questionList"
}, {
"$match": {
"questionList.type": "hard"
}
}, {
"$group": {
"_id": "$_id",
"testpaperid": {
"$first": "$testpaperid"
},
"testpaperNo": {
"$first": "$testpaperNo"
},
"questionList": {
"$push": "$questionList"
}
}
}).pretty()
java中的聚合查询如下所示:
BasicDBObject eleMatch = new BasicDBObject();
eleMatch.put("type", "hard");
BasicDBObject elemMatchQuery = new BasicDBObject();
elemMatchQuery.put("$elemMatch", eleMatch);
BasicDBObject query = new BasicDBObject();
query.put("questionList", elemMatchQuery);
BasicDBObject projection = new BasicDBObject();
projection.put("questionList.type.$", 1);
DBCollection dbcoll = mongoTemplate.getCollection("collectionName");
DBObject object = dbcoll.find(query, projection);
// unwind questionList
DBObject unwind = new BasicDBObject("$unwind", "$questionList");
// create pipeline operations, with the $match
DBObject match = new BasicDBObject("$match", new BasicDBObject("questionList.type", "hard"));
// Now the $group operation
DBObject groupFields = new BasicDBObject("_id", "$group field");
groupFields.put("testpaperid", new BasicDBObject("$first", "$testpaperid"));
groupFields.put("testpaperNo", new BasicDBObject("$first", "$testpaperNo"));
groupFields.put("questionList", new BasicDBObject("$push", "$questionList"));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
List < DBObject > pipeline = Arrays.asList(unwind, match, group);
AggregationOutput output = collectionName.aggregate(pipeline);
for(DBObject result: output.results()) {
System.out.println(result);
}
//展开问题列表
DBObject unwind=newbasicdbobject(“$unwind”,“$questionList”);
//使用$match创建管道操作
DBObject match=newbasicdbobject($match),newbasicdbobject(“questionList.type”,“hard”);
//现在是$group操作
DBObject groupFields=newbasicdbobobject(“\u id”,“$group field”);
groupFields.put(“testpaperid”,新的BasicDBObject(“$first”,“$testpaperid”));
groupFields.put(“testpaperNo”,新的BasicDBObject(“$first”,“$testpaperNo”);
groupFields.put(“问题列表”,新的BasicDBObject($push“,$questionList”);
DBObject group=新的BasicDBObject(“$group”,groupFields);
//运行聚合
Listpipeline=Arrays.asList(展开、匹配、组);
AggregationOutput输出=collectionName.aggregate(管道);
for(DBObject结果:output.results()){
系统输出打印项次(结果);
}
如果使用运算符,则会限制和mongo聚合查询的内容,如下所示:
db.collectionName.aggregate({
"$unwind": "$questionList"
}, {
"$match": {
"questionList.type": "hard"
}
}, {
"$group": {
"_id": "$_id",
"testpaperid": {
"$first": "$testpaperid"
},
"testpaperNo": {
"$first": "$testpaperNo"
},
"questionList": {
"$push": "$questionList"
}
}
}).pretty()
java中的聚合查询如下所示:
BasicDBObject eleMatch = new BasicDBObject();
eleMatch.put("type", "hard");
BasicDBObject elemMatchQuery = new BasicDBObject();
elemMatchQuery.put("$elemMatch", eleMatch);
BasicDBObject query = new BasicDBObject();
query.put("questionList", elemMatchQuery);
BasicDBObject projection = new BasicDBObject();
projection.put("questionList.type.$", 1);
DBCollection dbcoll = mongoTemplate.getCollection("collectionName");
DBObject object = dbcoll.find(query, projection);
// unwind questionList
DBObject unwind = new BasicDBObject("$unwind", "$questionList");
// create pipeline operations, with the $match
DBObject match = new BasicDBObject("$match", new BasicDBObject("questionList.type", "hard"));
// Now the $group operation
DBObject groupFields = new BasicDBObject("_id", "$group field");
groupFields.put("testpaperid", new BasicDBObject("$first", "$testpaperid"));
groupFields.put("testpaperNo", new BasicDBObject("$first", "$testpaperNo"));
groupFields.put("questionList", new BasicDBObject("$push", "$questionList"));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
List < DBObject > pipeline = Arrays.asList(unwind, match, group);
AggregationOutput output = collectionName.aggregate(pipeline);
for(DBObject result: output.results()) {
System.out.println(result);
}
//展开问题列表
DBObject unwind=newbasicdbobject(“$unwind”,“$questionList”);
//使用$match创建管道操作
DBObject match=newbasicdbobject($match),newbasicdbobject(“questionList.type”,“hard”);
//现在是$group操作
DBObject groupFields=newbasicdbobobject(“\u id”,“$group field”);
groupFields.put(“testpaperid”,新的BasicDBObject(“$first”,“$testpaperid”));
groupFields.put(“testpaperNo”,新的BasicDBObject(“$first”,“$testpaperNo”);
groupFields.put(“问题列表”,新的BasicDBObject($push“,$questionList”);
DBObject group=新的BasicDBObject(“$group”,groupFields);
//运行聚合
Listpipeline=Arrays.asList(展开、匹配、组);
AggregationOutput输出=collectionName.aggregate(管道);
for(DBObject结果:output.results()){
系统输出打印项次(结果);
}
如果使用运算符,则会限制和mongo聚合查询的内容,如下所示:
db.collectionName.aggregate({
"$unwind": "$questionList"
}, {
"$match": {
"questionList.type": "hard"
}
}, {
"$group": {
"_id": "$_id",
"testpaperid": {
"$first": "$testpaperid"
},
"testpaperNo": {
"$first": "$testpaperNo"
},
"questionList": {
"$push": "$questionList"
}
}
}).pretty()
java中的聚合查询如下所示:
BasicDBObject eleMatch = new BasicDBObject();
eleMatch.put("type", "hard");
BasicDBObject elemMatchQuery = new BasicDBObject();
elemMatchQuery.put("$elemMatch", eleMatch);
BasicDBObject query = new BasicDBObject();
query.put("questionList", elemMatchQuery);
BasicDBObject projection = new BasicDBObject();
projection.put("questionList.type.$", 1);
DBCollection dbcoll = mongoTemplate.getCollection("collectionName");
DBObject object = dbcoll.find(query, projection);
// unwind questionList
DBObject unwind = new BasicDBObject("$unwind", "$questionList");
// create pipeline operations, with the $match
DBObject match = new BasicDBObject("$match", new BasicDBObject("questionList.type", "hard"));
// Now the $group operation
DBObject groupFields = new BasicDBObject("_id", "$group field");
groupFields.put("testpaperid", new BasicDBObject("$first", "$testpaperid"));
groupFields.put("testpaperNo", new BasicDBObject("$first", "$testpaperNo"));
groupFields.put("questionList", new BasicDBObject("$push", "$questionList"));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
List < DBObject > pipeline = Arrays.asList(unwind, match, group);
AggregationOutput output = collectionName.aggregate(pipeline);
for(DBObject result: output.results()) {
System.out.println(result);
}
//展开问题列表
DBObject unwind=newbasicdbobject(“$unwind”,“$questionList”);
//使用$match创建管道操作
DBObject match=newbasicdbobject($match),newbasicdbobject(“questionList.type”,“hard”);
//现在是$group操作
DBObject groupFields=newbasicdbobobject(“\u id”,“$group field”);
groupFields.put(“testpaperid”,新的BasicDBObject(“$first”,“$testpaperid”));
groupFields.put(“testpaperNo”,新的BasicDBObject(“$first”,“$testpaperNo”);
groupFields.put(“问题列表”,新的BasicDBObject($push“,$questionList”);
DBObject group=新的BasicDBObject(“$group”,groupFields);
//运行聚合
Listpipeline=Arrays.asList(展开、匹配、组);
AggregationOutput输出=collectionName.aggregate(管道);
for(DBObject结果:output.results()){
系统输出打印项次(结果);
}
如果使用运算符,则会限制和mongo聚合查询的内容,如下所示:
db.collectionName.aggregate({
"$unwind": "$questionList"
}, {
"$match": {
"questionList.type": "hard"
}
}, {
"$group": {
"_id": "$_id",
"testpaperid": {
"$first": "$testpaperid"
},
"testpaperNo": {
"$first": "$testpaperNo"
},
"questionList": {
"$push": "$questionList"
}
}
}).pretty()
java中的聚合查询如下所示:
BasicDBObject eleMatch = new BasicDBObject();
eleMatch.put("type", "hard");
BasicDBObject elemMatchQuery = new BasicDBObject();
elemMatchQuery.put("$elemMatch", eleMatch);
BasicDBObject query = new BasicDBObject();
query.put("questionList", elemMatchQuery);
BasicDBObject projection = new BasicDBObject();
projection.put("questionList.type.$", 1);
DBCollection dbcoll = mongoTemplate.getCollection("collectionName");
DBObject object = dbcoll.find(query, projection);
// unwind questionList
DBObject unwind = new BasicDBObject("$unwind", "$questionList");
// create pipeline operations, with the $match
DBObject match = new BasicDBObject("$match", new BasicDBObject("questionList.type", "hard"));
// Now the $group operation
DBObject groupFields = new BasicDBObject("_id", "$group field");
groupFields.put("testpaperid", new BasicDBObject("$first", "$testpaperid"));
groupFields.put("testpaperNo", new BasicDBObject("$first", "$testpaperNo"));
groupFields.put("questionList", new BasicDBObject("$push", "$questionList"));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
List < DBObject > pipeline = Arrays.asList(unwind, match, group);
AggregationOutput output = collectionName.aggregate(pipeline);
for(DBObject result: output.results()) {
System.out.println(result);
}
//展开问题列表
DBObject unwind=newbasicdbobject(“$unwind”,“$questionList”);
//使用$match创建管道操作
DBObject match=newbasicdbobject($match),newbasicdbobject(“questionList.type”,“hard”);
//现在是$group操作
DBObject groupFields=newbasicdbobobject(“\u id”,“$group field”);
groupFields.put(“testpaperid”,新的BasicDBObject(“$first”,“$testpaperid”));
groupFields.put(“testpaperNo”,新的BasicDBObject(“$first”,“$testpaperNo”);
groupFields.put(“问题列表”,新的BasicDBObject($push“,$questionList”);
DBObject group=新的BasicDBObject(“$group”,groupFields);
//运行聚合
Listpipeline=Arrays.asList(展开、匹配、组);
AggregationOutput输出=collectionName.aggregate(管道);
for(DBObject结果:output.results()){
系统输出打印项次(结果);
}
试试看