【问题标题】:$project does not work properly$project 不能正常工作
【发布时间】:2015-01-06 18:56:15
【问题描述】:

我在 mongo 中有一个集合如下:

{
"_id" : ObjectId("5490b272f315dce7077204af"),
"Date" : ISODate("2014-10-19T04:00:00.000Z"),
"Type" : "Twitter",
"Entities" : [ 
    {
        "ID" : 2,
        "Name" : "test1",
        "Sentiment" : {
            "Value" : 19,
            "Neutral" : 1
        },
        "Quality" : {
            "Value" : 0.1,
            "Low" : 1
        },
        "Intensity" : {
            "Value" : 0,
            "Low" : 1
        },
        "Happiness" : {
            "Value" : 0.5,
            "Medium" : 1
        }
    }, 
    {
        "ID" : 4,
        "Name" : "test1",
        "Sentiment" : {
            "Value" : 10,
            "Neutral" : 1
        },
        "Quality" : {
            "Value" : 0.1,
            "Low" : 1
        },
        "Intensity" : {
            "Value" : 0,
            "Low" : 1
        },
        "Happiness" : {
            "Value" : 0.5,
            "Medium" : 1
        }
    }
]

}

现在我想按 Date 上的所有内容进行分组并获得 Sentiment.Value 的总和,我有一个 Java 代码如下,它工作得非常好:

ArrayList<DBObject> andArray = andArrayEntityIdsEqualAndDateBetweenGraph(entityIds, startDate, endDate);
    DBObject where = new BasicDBObject("$match", new BasicDBObject("$and", andArray));
    DBObject unwind = new BasicDBObject("$unwind", "$Entities"); // "$unwind" converts object with array into many duplicate objects, each with one from array
    collectionG = db.getCollection("GraphDataCollection");
    DBObject groupFields = new BasicDBObject( "_id", "$Date");
   groupFields.put("value", new BasicDBObject( "$sum", "$Entities.Sentiment.Value"));
    DBObject groupBy = new BasicDBObject("$group", groupFields );
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
    stages.add(where);
    stages.add(unwind);
    stages.add(groupBy);
    stages.add(sort);
    AggregationOutput output = collectionG.aggregate(stages);
    System.out.println(output.results());

结果如下:

[
{
    "_id": {
        "$date": "2014-10-19T04:00:00.000Z"
    },
    "value": 29
},
{
    "_id": {
        "$date": "2014-10-20T04:00:00.000Z"
    },
    "value": 20
},
{
    "_id": {
        "$date": "2014-10-21T04:00:00.000Z"
    },
    "value": 21
}

]

现在我想要隐藏 _id 并只显示日期和值,所以我将代码更改为以下内容:

DBObject where = new BasicDBObject("$match", new BasicDBObject("$and", andArray));
    DBObject unwind = new BasicDBObject("$unwind", "$Entities"); // "$unwind" converts object with array into many duplicate objects, each with one from array
    collectionG = db.getCollection("GraphDataCollection");
    DBObject groupFields = new BasicDBObject( "_id", "$Date");
   groupFields.put("value", new BasicDBObject( "$sum", "$Entities.Sentiment.Value"));
    DBObject groupBy = new BasicDBObject("$group", groupFields );
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));
    stages.add(where);
    stages.add(unwind);
    stages.add(groupBy);
    DBObject project = new BasicDBObject("_id",0);
     project.put("Date",1);
     project.put("value",1);
     project.put("Type",1);
     stages.add(new BasicDBObject("$project",project));
    stages.add(sort);
    AggregationOutput output = collectionG.aggregate(stages);
    System.out.println(output.results());

现在我希望隐藏 _id 但值和日期可见,但我不知道为什么会得到以下结果:

[
{
    "value": 29
},
{
    "value": 21
},
{
    "value": 20
}

]

谁能帮忙?

【问题讨论】:

    标签: mongodb mongodb-query mongodb-java


    【解决方案1】:

    更改您的相关project 对象以排除_id 并将_id 字段投影为Date 字段。

      DBObject project = new BasicDBObject("_id",0);
      project.put("Date","$_id");
      project.put("value",1);
    

    当你这样做时,

     DBObject project = new BasicDBObject("_id",0);
     project.put("Date",1);
     project.put("value",1);
     project.put("Type",1);
    

    project.put("Date",1) 无效,因为从$group 阶段进入$project 阶段的文档没有Date 字段,但它们的_id 字段中选择了日期。

    project.put("Type",1) 无效,因为原始文档有Type 字段,但分组后进入$project 阶段的文档没有。

    【讨论】:

    • 谢谢你解决了我的问题,只是一个简单的问题:由于 Date 在 mongo 中是 dateformat,因此返回如下: "Date" : { "$date" : "2014-10-19T04:00:00.000 Z"} 并且当我尝试使用以下代码在 java 中获取结果时: for (DBObject result : output.results()) { System.out.println(result.get("Date").toString());我得到这样转换的日期:Sun Oct 19 01:00:00 ADT 2014 有什么办法可以像原来的那样得到它(2014-10-19T04:00:00.000Z)?
    • @HamedMinaee 我可以在此评论中回答这个问题,但它有点宽泛。对此感到抱歉。如果我将其包含在我的答案中,那么它就会脱离原始问题的上下文。你能问一个新问题吗?这对面临同样问题的其他人也会有所帮助。
    • @HamedMinaee - 但您可以使用日期格式化 API,如 simpledateformat 来实现。
    • 当然原来的问题就在这里,你可以看看::stackoverflow.com/questions/27803876/…
    • 你好 BatScream ,这里有一个问题我真的很困惑,你能看看:stackoverflow.com/questions/28255100/…
    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 2010-11-30
    • 2021-10-02
    • 2017-08-06
    • 2013-11-22
    • 1970-01-01
    • 2013-09-10
    • 2017-04-05
    相关资源
    最近更新 更多