$project 不能正常工作
$project does not work properly
我在 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
}
}
]
}
现在我想按日期对所有内容进行分组并得到 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
}
]
有人可以帮忙吗?
更改相关 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
阶段的文档没有。
我在 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
}
}
]
}
现在我想按日期对所有内容进行分组并得到 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
}
]
有人可以帮忙吗?
更改相关 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
阶段的文档没有。