MongoDB-连接两个查询结果和density_rank

时间:2020-01-22 02:12:53

标签: mongodb join dense-rank

我正在学习MongoDB,但在理解其概念时遇到了一些问题。

我有一个看起来像这样的收藏集:

window.onMessage.listen((onData) {
  // do things
  if (messageEvent.data["id"] == property.id) { }
})

这是Enron数据库。

我正在尝试进行查询,该查询将返回列出的电子邮件以及其发送的邮件数量和接收的邮件数量。

我设法做了两个看起来像这样的查询:

db.email.findOne()
{
        "_id" : ObjectId("52af48b5d55148fa0c199646"),
        "sender" : "tori.wells@enron.com",
        "recipients" : [
                "michael@optsevents.com"
        ],
        "cc" : [ ],
        "text" : "Mr. Christman:\n\nThank you for your invitation for Dr. Lay to speak at your upcoming forum in \nFrance, the format looks wonderful.  Unfortunately, Dr. Lay has calendar \nconflicts and will be unable to participate.\n\nIf you should need further assistance, please do not hesitate to contact us.\n\nTori Wells\nExecutive Assistant",
        "mid" : "22263156.1075840285610.JavaMail.evans@thyme",
        "fpath" : "enron_mail_20110402/maildir/lay-k/_sent/101.",
        "bcc" : [ ],
        "to" : [
                "michael@optsevents.com"
        ],
        "replyto" : null,
        "ctype" : "text/plain; charset=us-ascii",
        "fname" : "101.",
        "date" : "2000-08-04 09:04:00-07:00",
        "folder" : "_sent",
        "subject" : "Wall Street Journal Millennium Forum"
}

如您所见,第一个返回给我的电子邮件和发送的电子邮件数量,第二个也返回给我的电子邮件和接收的邮件数量。

我的意思是将这两个结合起来(?)并得到一个查询,该查询将返回类似以下内容的内容:

db.email.aggregate({$group:{_id:"$sender",SendsAmount:{$sum:1}}},{$sort:{SendsAmount:-1}})
{ "_id" : "rosalee.fleming@enron.com", "SendsAmount" : 849 }
{ "_id" : "brown_mary_jo@lilly.com", "SendsAmount" : 82 }
{ "_id" : "leonardo.pacheco@enron.com", "SendsAmount" : 78 }

db.email.aggregate({$group:{_id:"$recipients",ReceivedAmount:{$sum:1}}},{$unwind:"$_id"},{$sort:{ReceivedAmount:-1}})
{ "_id" : "klay@enron.com", "ReceivedAmount" : 1350 }
{ "_id" : "kenneth.lay@enron.com", "ReceivedAmount" : 912 }
{ "_id" : "kenneth.lay@enron.com", "ReceivedAmount" : 78 }

我知道有$ lookup,但是它只能用于两个集合,所以我的想法是从这两个查询中创建两个集合,但是我觉得有更好的方法来解决我的问题。

---我的第二个问题是关于尝试执行MongoDB中不存在的DENSE_RANK。我想按已发送电子邮件的数量对电子邮件地址进行排名。

我使用了$ unwind和insertArrayIndex,但是却得到了类似ROW_NUMBER的内容,

我写了这样的东西:

{ "_id" : "email@enron.com", "SendsAmount" : 57, "ReceivedAmount": 43 }

给我想要的结果,但它甚至不是文档,而只是打印的信息。我已经读过有关MapReduce的信息,但我不知道如何在这种情况下使用它。

1 个答案:

答案 0 :(得分:0)

如果要在聚合查询中进行所有计算,则可以如下使用$ facet和$ group阶段

guard let info = Bundle.main.infoDictionary,
    let currentVersion = info["CFBundleShortVersionString"] as? String,
    let identifier = info["CFBundleIdentifier"] as? String,
    let url = URL(string: "http://itunes.apple.com/lookup?bundleId=\(identifier)") else {
    throw VersionError.invalidBundleInfo
}
let data = try Data(contentsOf: url)
guard let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any] else {
    throw VersionError.invalidResponse
}
if let result = (json["results"] as? [Any])?.first as? [String: Any], let version = result["version"] as? String {
    return version != currentVersion
}
throw VersionError.invalidResponse