英文:
Retrieve only the value of an arrays sub-document field using MongoDB Java
问题
我有一个数据库:
{
"_id": {
"$oid": "1"
},
"lastName": {
"lastName": "James"
},
"data": [{
"day": "20-09-2020 11:35:02",
"hoursWorked": "0小时:5分钟:321秒"
}, {
"day": "20-09-2020 11:35:29",
"hoursWorked": "0小时:5分钟:321秒"
}]
}
如何仅检索“data”字段的子字段“hoursWorked”的值。
因此,我只需要该字段的值或所有这些值,即0小时:5分钟:321秒。
我使用以下代码;然而,我仍然无法检索到该值。
Document document = collection
.find(new BasicDBObject("_id", y))
.projection(Projections.fields(Projections.include("data.hoursWorked"), excludeId())).first();
System.out.println(document);
}
英文:
I have a database:
{
"_id": {
"$oid": "1"
},
"lastName": {
"lastName": "James"
},
"data": [{
"day": "20-09-2020 11:35:02",
"hoursWorked": "0 hours : 5 mins : 321 secs"
}, {
"day": "20-09-2020 11:35:29",
"hoursWorked": "0 hours : 5 mins : 321 secs"
}]
}
How do I retrieve only the value of subfield "hoursWorked" of "data" field.
So, I need only the value or all values of such field, 0 hours : 5 mins : 321 secs.
I use the following; however, I still cannot retrieve the value.
Document document = collection
.find(new BasicDBObject("_id", y))
.projection(Projections.fields(Projections.include("data.hoursWorked"), excludeId())).first();
System.out.println(document);
}
答案1
得分: 1
使用 MongoDB Java 驱动程序:
List<Bson> pipeline =
Arrays.asList(
project(
fields(
excludeId(),
computed("hoursWorked",
eq("$reduce",
and(eq("input", "$data"), eq("initialValue", Arrays.asList()),
eq("in", eq("$concatArrays", Arrays.asList("$$value", Arrays.asList("$$this.hoursWorked"))))
)
)
)
)
),
unwind("$hoursWorked")
);
collection.aggregate(pipeline)
.into(new ArrayList<Document>())
.forEach(doc -> System.out.println(doc.get("hoursWorked")));
打印data数组中的两个值:
0小时:5分钟:321秒
0小时:5分钟:322秒
英文:
Using MongoDB Java Driver:
List<Bson> pipeline =
Arrays.asList(
project(
fields(
excludeId(),
computed("hoursWorked",
eq("$reduce",
and(eq("input", "$data"), eq("initialValue", Arrays.asList()),
eq("in", eq("$concatArrays", Arrays.asList("$$value", Arrays.asList("$$this.hoursWorked") ) ) )
)
)
)
)
),
unwind("$hoursWorked")
);
collection.aggregate(pipeline)
.into(new ArrayList<Document>())
.forEach(doc -> System.out.println(doc.get("hoursWorked")));
Prints two values from the data
array:
0 hours : 5 mins : 321 secs
0 hours : 5 mins : 322 secs
答案2
得分: 0
你可以使用投影 $project
。
db.collection.aggregate([
{
$project: {
"data.hoursWorked": 1
}
}
])
在 Mongo 游乐场 中运行
在 Spring Boot 中,你需要自动装配 mongoTemplate
:
@Autowired
MongoTemplate mongoTemplate;
聚合代码:
public List<Object> test(){
Aggregation aggregation = Aggregation.newAggregation(
a-> new Document("$project",
new Document("data.hoursWorked", 1)
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}
更新 1
使用 Bson.Document
:
Document nodes = (Document) collection
.find(new BasicDBObject("_id", "1"))
.projection(Projections.fields(Projections.include("data.hoursWorked"), Projections.excludeId()))
.first();
List<Document> list = (List<Document>) nodes.get("data");
for(Document d:list){
System.out.println(d.get("hoursWorked"));
}
英文:
You can use projection $project
.
db.collection.aggregate([
{
$project: {
"data.hoursWorked": 1
}
}
])
Working Mongo playground
In spring boot, you need to autowire the mongoTemplate
@Autowired
MongoTemplate mongoTemplate;
The aggregation code,
public List<Object> test(){
Aggregation aggregation = Aggregation.newAggregation(
a-> new Document("$project",
new Document("data.hoursWorked",1)
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}
Update 1
Using Bson.Doument
Document nodes = (Document) collection
.find(new BasicDBObject("_id", "1"))
.projection(Projections.fields(Projections.include("data.hoursWorked"), Projections.excludeId()))
.first();
List<Document> list = (List<Document>) nodes.get("data");
for(Document d:list){
System.out.println(d.get("hoursWorked"));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论