投影到 child 的字段
Projection to child's field
Spring 1.3.6,使用 spring-data mongodb
有这样的文档:
{
"name":"Dmitry",
"props":{
"city":"Moscow"
"age":"26"
}
}
想要这样的东西通过 spring mongo 投影
{
"city":"Moscow",
"person":{
"name":"Dmitry",
"age":"26"
}
}
试过这个聚合操作
Aggregation aggregation = newAggregation(
project().and("name").as("person.name")
.and("props.city").as("city")
.and("props.age").as("person.age")
);
AggregationResults<DBObject> results = this.mongoTemplate.aggregate(aggregation, MyType.class, DBObject.class);
results.getMappedResults();
有这样的结果
{
"city":"Moscow",
"name":"Dmitry",
"person":{
"age":"26"
}
}
可以将字段绑定为另一个名称的字段,将 child 字段绑定到 parent 字段,将 child 字段绑定到另一个 child,但是我没有成功通过 mongo 投影将 parent 字段绑定到 child。
打印生成的聚合查询,看看它是否匹配下面的查询。
db.Whosebug.aggregate([{$project:{"person.name": "$name", city:"$props.city", "person.age":"$props.age"}}]).pretty();
当我执行查询时,我得到了以下结果
{
"_id" : ObjectId("57b32a0fc516d5b5c94cae0d"),
"person" : {
"name" : "Dmitry",
"age" : "26"
},
"city" : "Moscow"
}
{
"_id" : ObjectId("57b32b59c516d5b5c94cae0e"),
"person" : {
"name" : "Roseline",
"age" : "22"
},
"city" : "Berlin"
}
这些是我的记录collection
db.Whosebug.find().pretty();
{
"_id" : ObjectId("57b32a0fc516d5b5c94cae0d"),
"name" : "Dmitry",
"props" : {
"city" : "Moscow",
"age" : "26"
}
}
{
"_id" : ObjectId("57b32b59c516d5b5c94cae0e"),
"name" : "Roseline",
"props" : {
"city" : "Berlin",
"age" : "22"
}
}
希望对您有所帮助!!
这是使用投影的 Spring 代码。您可能需要根据从 Spring 上下文获取 MongoOperations 对象的方式替换此代码 "getMongoConnection()"。
聚合方法使用投影:-
public Boolean aggregateMyTypeCollectionUsingProject() {
MongoOperations mongoOperations = getMongoConnection();
ProjectionOperation project = Aggregation.project().and("props.city").as("city").and("props.age").as("person.age").andExpression("name")
.as("person.name");
Aggregation aggregate = Aggregation.newAggregation(project);
System.out.println(aggregate.toString());
AggregationResults<DBObject> results = mongoOperations.aggregate(aggregate, MyType.class, DBObject.class);
System.out.println("Result ============>" + results.getMappedResults());
return true;
}
我的 getMongoConnection() 方法:-
@SuppressWarnings("resource")
public MongoOperations getMongoConnection() {
return (MongoOperations) new AnnotationConfigApplicationContext(SpringMongoConfig.class)
.getBean("mongoTemplate");
}
查询:-
{ "aggregate" : "__collection__" , "pipeline" : [ { "$project" : { "city" : "$props.city" , "person.age" : "$props.age" , "person.name" : "$name"}}]}
输出:-
{
"_id": {
"$oid": "57b32d31ced49443e4b79f0d"
},
"city": "Moscow",
"person": {
"age": "26",
"name": "Dmitry"
}
}
Spring 1.3.6,使用 spring-data mongodb
有这样的文档:
{
"name":"Dmitry",
"props":{
"city":"Moscow"
"age":"26"
}
}
想要这样的东西通过 spring mongo 投影
{
"city":"Moscow",
"person":{
"name":"Dmitry",
"age":"26"
}
}
试过这个聚合操作
Aggregation aggregation = newAggregation(
project().and("name").as("person.name")
.and("props.city").as("city")
.and("props.age").as("person.age")
);
AggregationResults<DBObject> results = this.mongoTemplate.aggregate(aggregation, MyType.class, DBObject.class);
results.getMappedResults();
有这样的结果
{
"city":"Moscow",
"name":"Dmitry",
"person":{
"age":"26"
}
}
可以将字段绑定为另一个名称的字段,将 child 字段绑定到 parent 字段,将 child 字段绑定到另一个 child,但是我没有成功通过 mongo 投影将 parent 字段绑定到 child。
打印生成的聚合查询,看看它是否匹配下面的查询。
db.Whosebug.aggregate([{$project:{"person.name": "$name", city:"$props.city", "person.age":"$props.age"}}]).pretty();
当我执行查询时,我得到了以下结果
{
"_id" : ObjectId("57b32a0fc516d5b5c94cae0d"),
"person" : {
"name" : "Dmitry",
"age" : "26"
},
"city" : "Moscow"
}
{
"_id" : ObjectId("57b32b59c516d5b5c94cae0e"),
"person" : {
"name" : "Roseline",
"age" : "22"
},
"city" : "Berlin"
}
这些是我的记录collection
db.Whosebug.find().pretty();
{
"_id" : ObjectId("57b32a0fc516d5b5c94cae0d"),
"name" : "Dmitry",
"props" : {
"city" : "Moscow",
"age" : "26"
}
}
{
"_id" : ObjectId("57b32b59c516d5b5c94cae0e"),
"name" : "Roseline",
"props" : {
"city" : "Berlin",
"age" : "22"
}
}
希望对您有所帮助!!
这是使用投影的 Spring 代码。您可能需要根据从 Spring 上下文获取 MongoOperations 对象的方式替换此代码 "getMongoConnection()"。
聚合方法使用投影:-
public Boolean aggregateMyTypeCollectionUsingProject() {
MongoOperations mongoOperations = getMongoConnection();
ProjectionOperation project = Aggregation.project().and("props.city").as("city").and("props.age").as("person.age").andExpression("name")
.as("person.name");
Aggregation aggregate = Aggregation.newAggregation(project);
System.out.println(aggregate.toString());
AggregationResults<DBObject> results = mongoOperations.aggregate(aggregate, MyType.class, DBObject.class);
System.out.println("Result ============>" + results.getMappedResults());
return true;
}
我的 getMongoConnection() 方法:-
@SuppressWarnings("resource")
public MongoOperations getMongoConnection() {
return (MongoOperations) new AnnotationConfigApplicationContext(SpringMongoConfig.class)
.getBean("mongoTemplate");
}
查询:-
{ "aggregate" : "__collection__" , "pipeline" : [ { "$project" : { "city" : "$props.city" , "person.age" : "$props.age" , "person.name" : "$name"}}]}
输出:-
{
"_id": {
"$oid": "57b32d31ced49443e4b79f0d"
},
"city": "Moscow",
"person": {
"age": "26",
"name": "Dmitry"
}
}