2015-10-07 91 views
2

我想在这里做一个简单的MongoDB聚合。基本上我有文件是这样的:Morphia展开聚合返回与Mongo数据库查询不同的结果吗?

{ 
timestampInMs: 1444094140442, 
records: [ 
{ 
value: "testvalue", 
experiment: { 
id: "56105b0af2763b25806d1365", 
name: "integrationtest-kkkk", 
created: "2015-10-03T22:47:38.479+0000", 
updated: null 
}, 
sensorId: "testsensor", 
dataType: 1 
}, 
{ 
value: "testvalue2", 
experiment: { 
id: "56105b0af2763b25806d1365", 
name: "integrationtest-kkkk", 
created: "2015-10-03T22:47:38.479+0000", 
updated: null 
}, 
sensorId: "testsensor2", 
dataType: 1 
}, 
{ 
value: "testvalue3", 
experiment: { 
id: "56105b0af2763b25806d1365", 
name: "integrationtest-kkkk", 
created: "2015-10-03T22:47:38.479+0000", 
updated: null 
}, 
sensorId: "testsensor3", 
dataType: 1 
}, 
{ 
value: "testvalue4", 
experiment: { 
id: "56105b0af2763b25806d1365", 
name: "integrationtest-kkkk", 
created: "2015-10-03T22:47:38.479+0000", 
updated: null 
}, 
sensorId: "testsensor4", 
dataType: 1 
} 
], 
created: "2015-10-06T01:15:40.501+0000", 
updated: "2015-10-06T01:15:40.528+0000" 
} 

Java模型看起来像这样

@Entity("sensordatadocs") 
@Indexes ({ 
    @Index(fields = @Field("timestampInMs"), options = @IndexOptions(name = "timestamp_ms_index")) 
}) 
public class DbSensorDataDocument { 

    @Id 
    // Should be milliseconds since Epoch 
    private Long timestampInMs; 

    @Embedded 
    private List<DbSensorDataRecord> records; 

    private Date created; 

    private Date updated; 
} 

public class DbSensorDataRecord { 

    private String value; 

    @Reference 
    private Experiment experiment; 

    private String sensorId; 

    private int dataType; 
} 

当我使用DB查询

db.sensordatadocs.aggregate([{$unwind: "$records"}]) 

它给了我4号文件,每个文件的“纪录“包含与原始数组中的项目对应的单个项目。然而,当我使用吗啡的API,像这样:

Iterator<DbSensorDataDocument> iter = datastore.createAggregation(DbSensorDataDocument.class) 
     .unwind("records").aggregate(DbSensorDataDocument.class); 

它会返回结果是这样的:

{ 
timestampInMs: 1444094140442, 
sensorId: "testsensor", 
value: "testvalue", 
experimentId: "56105b0af2763b25806d1365", 
dataType: 1 
}, 
{ 
timestampInMs: 1444094140442, 
sensorId: "testsensor", 
value: "testvalue", 
experimentId: "56105b0af2763b25806d1365", 
dataType: 1 
}, 
{ 
timestampInMs: 1444094140442, 
sensorId: "testsensor", 
value: "testvalue", 
experimentId: "56105b0af2763b25806d1365", 
dataType: 1 
}, 
{ 
timestampInMs: 1444094140442, 
sensorId: "testsensor", 
value: "testvalue", 
experimentId: "56105b0af2763b25806d1365", 
dataType: 1 
} 

这是我的迭代的代码看起来像:

while (iter.hasNext()) { 
      DbSensorDataDocument doc = iter.next(); 
      final SensorDataRecord record = SensorDataUtils.flattenSensorDataDocument(doc); 
      result.add(record); 
     } 

注意项目数量是正确的,但是,“记录”内的值不正确,实际上它们只是原始数组中**第一个项目的值* *。为什么会这样?请帮忙。谢谢!

吗啡版本1.0.1

+0

这听起来很愚蠢,但你确定迭代是否正确完成? – xeraa

+0

我已更新帖子中的迭代代码。应该是正确的? O.O – littlejedi

回答

0

这种行为的原因是吗啡的缓存问题。在你的情况下,Morphia将DbSensorDataDocument实体的所有结果视为同一行(因为具有相同的主键timestampInMs),并且返回缓存版本

为了解决这个问题,你必须排除现场timestampInMs从聚集推算结果:

Iterator<DbSensorDataDocument> iter = datastore.createAggregation(DbSensorDataDocument.class) 
     .unwind("records").project(
Projection.projection("timestampInMs").suppress(), 
Projection.projection("XXXXXX").aggregate(DbSensorDataDocument.class); 

其中xxxxx是要包含的字段。