提供在 $project 中使用 mongoDB 指定字段顺序的方法
Provide way to specify field order in $project on using mongoDB
我正在尝试根据结果中特定顺序的字段 'Type' 从 Mongo 数据库中提取文档。结果被正确检索,但结果没有保留查询中给出的字段顺序。
我已经包含了下面的工作代码片段,有人可以帮助我如何根据我的代码片段更新等效的 Java 代码。
Mongo query is as below :- (Which retrieves data)
==========================================
db.getCollection('elements').aggregate([
{$match : {'Type' : {$eq : 'ALM_Project'}}},
{ $project: {
_id: 0,
'ID' : 1,
'Summary' : 1,
'Created Date' : 1
}},
])
Equivalent Java code is as below :-
==============================================================
AggregateIterable<Document> documents = getMongoCollection()
.aggregate(Arrays.asList(Aggregates.match(andQuery),
Aggregates.project(Projections.fields(
Projections.include("ID", "Summary", "Created Date"),
Projections.exclude("_id")))));
Mongo query is as below :- (Added the fields preserving code)
===============================================================
db.getCollection('elements').aggregate([
{$match : {'Type' : {$eq : 'ALM_Project'}}},
{ $project: {
_id: 0,
'ID' : 1,
'Summary' : 1,
'Created Date' : 1
}},
{ "$project": {
"ID": "$ID",
"Summary": "$Summary",
"Created Date": "$Created Date"
}}
])
Mongo 查询通过保留字段顺序完美运行。谁能告诉我如何基于此更新 java 代码。
您可以使用 MongoDB Compass 从管道输出 Java 代码。这是示例代码。
Arrays.asList(match(eq("Type", "ALM_Project")),
project(fields(excludeId(),
include("ID", "Summary", "Created Date"))),
project(computed("ID", "$ID"),
computed("Summary", "$Summary"),
computed("Created Date", "$Created Date")))
我正在尝试根据结果中特定顺序的字段 'Type' 从 Mongo 数据库中提取文档。结果被正确检索,但结果没有保留查询中给出的字段顺序。
我已经包含了下面的工作代码片段,有人可以帮助我如何根据我的代码片段更新等效的 Java 代码。
Mongo query is as below :- (Which retrieves data)
==========================================
db.getCollection('elements').aggregate([
{$match : {'Type' : {$eq : 'ALM_Project'}}},
{ $project: {
_id: 0,
'ID' : 1,
'Summary' : 1,
'Created Date' : 1
}},
])
Equivalent Java code is as below :-
==============================================================
AggregateIterable<Document> documents = getMongoCollection()
.aggregate(Arrays.asList(Aggregates.match(andQuery),
Aggregates.project(Projections.fields(
Projections.include("ID", "Summary", "Created Date"),
Projections.exclude("_id")))));
Mongo query is as below :- (Added the fields preserving code)
===============================================================
db.getCollection('elements').aggregate([
{$match : {'Type' : {$eq : 'ALM_Project'}}},
{ $project: {
_id: 0,
'ID' : 1,
'Summary' : 1,
'Created Date' : 1
}},
{ "$project": {
"ID": "$ID",
"Summary": "$Summary",
"Created Date": "$Created Date"
}}
])
Mongo 查询通过保留字段顺序完美运行。谁能告诉我如何基于此更新 java 代码。
您可以使用 MongoDB Compass 从管道输出 Java 代码。这是示例代码。
Arrays.asList(match(eq("Type", "ALM_Project")),
project(fields(excludeId(),
include("ID", "Summary", "Created Date"))),
project(computed("ID", "$ID"),
computed("Summary", "$Summary"),
computed("Created Date", "$Created Date")))