为什么MapReduce为文档中存在的字段返回undefined?

时间:2017-04-19 19:53:31

标签: mongodb mapreduce

我正在尝试调试我在集合上运行mapreduce时遇到的一个奇怪的问题:

作为参考,这里有来自该集合的单个文件:

{
    "_id" : "ITOUXFWgvWs",
    "source" : "youtube",
    "insert_datetime" : ISODate("2017-04-06T22:27:43.598Z"),
    "processed" : false,
    "raw" : {
        "id" : "ITOUXFWgvWs",
        "etag" : "\"m2yskBQFythfE4irbTIeOgYYfBU/hiQtS6aptLlqxTpsYp1EJIRcoZo\"",
        "snippet" : {
            "publishedAt" : ISODate("2017-04-06T13:25:28Z"),
            "title" : "Alarm.com: The Only Smart Home App You Need",
            "channelId" : "UC_HZfoZUP36STk7SrtKYH4g",
            "description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home?  It can all come together with Alarm.com.",
            "categoryId" : "28",
            "channelTitle" : "Alarm.com",
            "thumbnails" : {
                "default" : {
                    "height" : 90,
                    "width" : 120,
                    "url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/default.jpg"
                },
                "standard" : {
                    "height" : 480,
                    "width" : 640,
                    "url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/sddefault.jpg"
                },
                "high" : {
                    "height" : 360,
                    "width" : 480,
                    "url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/hqdefault.jpg"
                },
                "medium" : {
                    "height" : 180,
                    "width" : 320,
                    "url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/mqdefault.jpg"
                },
                "maxres" : {
                    "height" : 720,
                    "width" : 1280,
                    "url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/maxresdefault.jpg"
                }
            },
            "liveBroadcastContent" : "none",
            "localized" : {
                "title" : "Alarm.com: The Only Smart Home App You Need",
                "description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home?  It can all come together with Alarm.com."
            }
        },
        "contentDetails" : {
            "duration" : "PT37S",
            "dimension" : "2d",
            "definition" : "hd",
            "licensedContent" : false,
            "projection" : "rectangular",
            "caption" : "false"
        },
        "kind" : "youtube#video",
        "statistics" : {
            "likeCount" : "0",
            "dislikeCount" : "0",
            "favoriteCount" : "0",
            "viewCount" : "32"
        },
        "uploaded" : ISODate("2017-04-06T13:25:28Z")
    },
}

我真的遵循官方mongo文档中的mapreduce调试步骤。

这是我的mapreduce脚本的样子:

var map = function() {
    emit("1", this._id);
};

var emit = function(key, value) {
    print("emit");
    print("key: " + key + "  value: " + tojson(value));
}

var myDoc = db.getCollection("abc").find({}).limit(1);
map.apply(myDoc);

它总是产生这样的结果:

MongoDB shell version: 2.4.6
connecting to: test
emit
key: 1  value: undefined

我希望脚本发出_id,因为它显然存在于文档中,但它没有。

可能的原因是什么?

1 个答案:

答案 0 :(得分:0)

find()始终返回游标。

将其替换为findOne()

var myDoc = db.getCollection("abc").findOne({});

或使用toArray()

将文档存储在数组中
var myDoc = db.getCollection("abc").find({}).limit(1).toArray()[0];