为什么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,因为文档中显然存在,但不存在。

这可能是什么原因?

find()总是返回光标。

findOne()

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

或使用toArray()

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

最新更新