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

Mongodb 为什么MapReduce为文档中存在的字段返回undefined?,mongodb,mapreduce,Mongodb,Mapreduce,我试图调试在集合上运行mapreduce时遇到的一个奇怪问题: { "_id" : "ITOUXFWgvWs", "source" : "youtube", "insert_datetime" : ISODate("2017-04-06T22:27:43.598Z"), "processed" : false, "raw" : { "id" : "ITOUXFWgvWs", "etag" : "\"m2yskBQFythfE

我试图调试在集合上运行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")
    },
}
以下是本系列的一份文档供参考:

{
    "_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
我希望脚本发出
\u id
,因为它显然存在于文档中,但它不存在

可能的原因是什么?

find()
始终返回光标

将其替换为
findOne()

或者使用
toArray()

find()
始终返回光标

将其替换为
findOne()

或者使用
toArray()


尝试
var myDoc=db.getCollection(“abc”).findOne()
as
find()
返回一个cursorTry
var myDoc=db.getCollection(“abc”).findOne()
as
find()
返回一个游标,如果我一次需要所有记录,我的意思是如果有超过1条的记录。
db.getCollection(“abc”)。查找({})。toArray()
并在map函数中迭代数组中的值。只是好奇而已。你为什么想得到所有的记录?您可以使用map reduce中的
query
字段来通过限制进入map reduce管道的文档的条件。只有标题显示它是MapReduce。我在任何地方都看不到它的实现,如果我想一次获得所有记录,我的意思是如果有超过1个.db.getCollection(“abc”).find({}).toArray()并在map函数中迭代数组中的值。只是好奇而已。你为什么想得到所有的记录?您可以使用map reduce中的
query
字段来通过限制进入map reduce管道的文档的条件。只有标题显示它是MapReduce。我看不到它在任何地方得到实施
var myDoc = db.getCollection("abc").find({}).limit(1).toArray()[0];