我使用Mongo v2.2.0。
我写了查询,但主要问题是$ arrayElemAt。使用$ unwind- $ first进行标准替换对我来说不起作用,我认为存在更好的解决方案。我有限制运行此聚合管道作为一个单一的操作,而不是运行查询积极和负数数据和后来合并结果的代码。我需要对结果查询应用$ sort,$ limit和$ skip,以限制计数words
用于过滤来自其他集合的记录,并在Java代码中合并来自两个集合的数据。
聚集查询:
[
{
$match: {
"merchantId": ObjectId("59520e6ccc7a701fbed31f94"),
"date": {
"$gte": NumberLong(1389644800000),
"$lt": NumberLong(1502409599999)
},
"isbn": "a123",
}
},
{
$project: {
"word": 1,
"sentence": 1,
"type": 1,
"date": 1
}
},
{
$sort: {
"date": -1
}
},
{
$group: {
"_id": {
"word": "$word",
"type": "$type"
},
"date": {
$max: "$date"
},
"sentence": {
$first: "$sentence"
},
"sentenceCount": {
"$sum": 1
}
},
},
{
$group: {
"_id": "$_id.word",
"word": { $first: "$_id.word"},
"positiveCount": {$sum: {$cond: [{$eq: ["$_id.type", "positive"]}, "$sentenceCount", 0]}},
"count": {$sum: "$sentenceCount"},
"positiveSentence": {
"$push": {
"$cond": [{$eq: ["$_id.type", "positive"]}, "$sentence", "$noval"]
}
},
"negativeSentence": {
"$push": {
"$cond": [{$eq: ["$_id.type", "negative"]}, "$sentence", "$noval"]
}
}
}
},
{
$project: {
"_id": 0,
"word": 1,
"sentimentPercentage": {$cond: [{$eq: ["$count", 0]}, 0, {$multiply: [{$divide: ["$positiveCount", "$count"]}, 100]}]},
"positiveSentence": {$arrayElemAt: ["$positiveSentence", 0]},
"negativeSentence": {$arrayElemAt: ["$negativeSentence", 0]},
}
},
{
$sort: {
sentimentPercentage: -1
}
},
{
$limit: 50
}
]
收集文件 “纲目”:
{
"_id" : ObjectId("59887424e4b099e00724aa44"),
"merchantId" : ObjectId("59520e6ccc7a701fbed31f94"),
"isbn" : "a123",
"sentence" : "Great, friendly service.",
"word" : "service",
"type" : "positive",
"date" : NumberLong(1466809200000),
}
预期输出:
{
"word" : "expectations",
"sentimentPercentage" : 100.0,
"positiveSentence" : "The service exceeded our expectations."
},
{
"word" : "representative",
"sentimentPercentage" : 87.5,
"positiveSentence" : "Excellent local representative, met the flight and gave us all the relevant information to ensure a great holiday.",
"negativeSentence" : "The representative at resort was poor."
},
{
"word" : "seats",
"sentimentPercentage" : 0.0,
"negativeSentence" : "Long delay and pre booked seats were lost ."
}
请,你能告诉我如何替换$ arrayElemAt运营商或甚至更好的如何使用Mongo的功能来优化这个查询到所需的输出= 2.2.0?
你能否提供一些样本数据和你想要的输出? – dnickless
@无需我提供了预期输出的示例 – Ray