为什么 MapReduce return 未定义文档中存在的字段?

Why does MapReduce return undefined for a field that exists in document?

我正在尝试调试我在 运行 遇到的一个奇怪问题,而 运行 对集合进行 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 因为它显然存在于文档中,但它不存在。

这可能是什么原因造成的?

find() 总是 returns 游标。

替换为findOne()

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

或者使用 toArray()

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