mongodb - mongodb mapreduce 函数是否接受文档的计算值作为映射器

标签 mongodb mongoose mongodb-query

我使用 MongoDB 2.6.10 版。下面是集合结构。我使用 MapReduce 函数根据(创建(不包括秒))、事件名称对用户名称进行分组。

{
    "_id" : ObjectId("59c11d79078dc54153c36ee8"),
    "event_name" : "notification",
    "created" : ISODate("2017-09-19T13:36:57.252Z"),
    "sender_name" : "nathan",
    "user_name": "Ragul"",
}
{
    "_id" : ObjectId("59c11d79078dc54153c36eeb"),
    "event_name" : "notification",
    "created" : ISODate("2017-09-19T13:36:57.772Z"),
    "sender_name" : "parmesh",
    "user_name": "Ram",
}
{
    "_id" : ObjectId("59c11d7a078dc54153c36ef0"),
    "event_name" : "notification",
    "created" : ISODate("2017-09-19T13:36:58.554Z"),
    "sender_name" : "nathan",
    "user_name": "Ram",

}
{
    "_id" : ObjectId("59c11d7a078dc54153c36ef1"),
    "event_name" : "message",
    "created" : ISODate("2017-09-19T13:36:58.577Z"),
    "sender_name" : "nathan",
    "user_name": "Ragul"",
}

下面是我使用 MapReduce 函数的查询。 我的问题是我们是否可以使用计算日期作为映射器。帮助我提出你的建议

var mapfn = function(){
    if (this.event_name == "message"){
        name = this.recipient_name
    }
    else if ((this.event_name == "notification") && (this.other_status == true)){
        name = this.sender_name
    }
    else if ((this.event_name == "notification") && (this.other_status == false)){
        name = "You"
    }
    this.cre = {$subtract:[this.created,{$add:[{$multiply:[{$second:this.created},1000]},{$millisecond:this.created}]}]}
    emit({"event_name": this.event_name, "created": this.cre}, name)
}

var redfun = function(key, value){
    return Array.append(value)
}

db.getCollection('users').mapReduce(mapfn, redfun, {out: "example"}).find()

最佳答案

我在这里不是使用 MongoDB 表达式计算日期,而是尝试使用 javascript 来消除秒数,然后我映射然后它起作用了。

var mapfn = function(){
    if (this.event_name == "message"){
        name = this.recipient_name
    }
    else if ((this.event_name == "notification") && (this.other_status == true)){
        name = this.sender_name
    }
    else if ((this.event_name == "notification") && (this.other_status == false)){
        name = "You"
    }
    this.created.setSeconds(0);
    this.created.setMilliseconds(0);
    emit({"event_name": this.event_name, "created": this.created}, name)
}

var redfun = function(key, value){
    var names = value.join(",")
    return names
}

db.users.mapReduce(mapfn, redfun, {out: "example"}).find()

关于mongodb - mongodb mapreduce 函数是否接受文档的计算值作为映射器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46365549/

相关文章:

java - 从上到下遍历具有两个以上子节点的树以获取java中的值,然后自下而上遍历以再次调整值

python - 如何用pymongo连接远程mongodb

javascript - 如何修复 "trim"在 Mongoose 模式中不起作用

mongodb - 如何在mongodb中查询对象的键?

php - 如何从 MongoDB 对象中提取时间戳

java - 如何解决错误: No server chosen by ReadPreferenceServerSelector

javascript - Mongodb父引用树获取当前位置和完整路径

javascript - Mongoose 无法识别我的 2dsphere 索引

mongodb - Golang MGO Group By multiple params 并按日期时间抓取最后一个

r - 在 R 中通过 SSH 连接到 MongoDB