【问题标题】:Morphia unwind aggregation returns different result than Mongo db query?Morphia 展开聚合返回与 Mongo db 查询不同的结果?
【发布时间】:2015-10-07 06:58:40
【问题描述】:

我在这里尝试做一个简单的 MongoDB 聚合。基本上我有这样的文件:

{
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.sensordatadocs.aggregate([{$unwind: "$records"}])

它给了我 4 个文档,每个文档的“记录”包含一个项目,它对应于原始数组中的项目。但是,当我使用 Morphia 的 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);
        }

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

Morphia 1.0.1 版

【问题讨论】:

  • 听起来很蠢,但你确定迭代正确吗?
  • 我已经更新了帖子中的迭代代码。应该是正确的? O.O

标签: java mongodb morphia


【解决方案1】:

这种行为的原因是 Morphia 缓存问题。在您的情况下,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 是您要包含的字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多