MapReduce返回null-涉及嵌套的字段/数组

时间:2019-04-12 16:00:20

标签: mongodb mapreduce

我正在编写一个mapreduce函数,以下是我现在所拥有的:

let input = [{"M": {"usernameFormatOptionsCreate": {"SS": ["sms"]}}}, {"M": {"UsernameFormatOptionsChange": {"SS": ["sms"]}}}];
let output = input.map(({M}) => {
  let y = {};
  Object.entries(M).forEach(([key, {SS}]) => {
    y[key] = SS;
  });
  return y;
});

console.log(output);

我看到此mapreduce的输出为空,如下所示:

function myMapper() {
    //The mapper function is called with each document, which has the special name 'this'
    //Emit a key-value pair:
    var hashtag;
    for (hashtag in this.entities.hashtags) {
      //print(hashtag.text);
      emit(hashtag.text, 1);
    }

    //emit(this.entities, 1);
}

function myReducer(key, values) {
    //The reducer is called once for each key, and is passed an array
    //containing all values corresponding to that key.
    //Produce the desired result
    return Array.sum( values );
}


db.tweets.mapReduce(myMapper, myReducer, { query: {}, out: "hashtags" })
db.hashtags.aggregate({$sort: {value: -1}})

我知道实体是一个字段,然后标签是一个数组,对吗?那么为什么这段代码不起作用?

{ "_id" : null, "value" : 454540 }

我对JavaScript并不是最有经验,但是在对mapreduce进行故障排除方面似乎非常有限。

更新:我觉得我现在已经尝试了一切。老实说,它必须是映射器代码,但我不确定它是什么。

以下是我一直在进行的“调试”:

{
    "_id" : ObjectId("5aa58befc4214f42f4c7cc70"),
    "created_at" : "Mon Apr 24 14:39:49 +0000 2017",
    "id" : NumberLong("856518043605905409"),
    "id_str" : "856518043605905409",
    "text" : "RT @Sheena_Davis: Lots of activities for Safety week \nMay 1-6 dfghdfghd #SafeCityLondon #ldnont",
    "source" : "<a href=\"bnmvnm" rel=\"nofollow\">Twitter Web Client</a>",
    "truncated" : false,
    "in_reply_to_status_id" : null,
    "in_reply_to_status_id_str" : null,
    "in_reply_to_user_id" : null,
    "in_reply_to_user_id_str" : null,
    "in_reply_to_screen_name" : null,
    "user" : {
        "id" : 83649318,
        "id_str" : "83649318",
        "name" : "MLHealthUnit",
        "screen_name" : "MLHealthUnit",
        "location" : "London and Middlesex County",
        "description" : "Official account of the Middlesex-London Health Unit │We promote and protect the health of our community. We’re HERE for YOU. #MLHU #LdnOnt",
        "protected" : false,
        "verified" : true,
        "followers_count" : 9937,
        "friends_count" : 3794,
        "listed_count" : 257,
        "favourites_count" : 6287,
        "profile_link_color" : "008265",
        "default_profile" : false,
        "default_profile_image" : false,
        "following" : null,
        "follow_request_sent" : null,
        "notifications" : null
    },
    "geo" : null,
    "coordinates" : null,
    "place" : null,
    "contributors" : null,
    "retweeted_status" : {
        "created_at" : "Mon Apr 24 10:24:21 +0000 2017",
        "id" : NumberLong("856453750915043328"),
        "id_str" : "856453750915043328",
        "text" : "Lots of activities for Safety week \nMay 1-6 ghjghj #SafeCityLondon #ldnont",
        "in_reply_to_status_id" : null,
        "in_reply_to_status_id_str" : null,
        "in_reply_to_user_id" : null,
        "in_reply_to_user_id_str" : null,
        "in_reply_to_screen_name" : null,
        "user" : {
            "id" : 144868126,
            "id_str" : "144868126",
            "name" : "Sheena Davis",
            "screen_name" : "Sheena_Davis",
            "location" : "London ON CA",
        },
        "entities" : {
            "hashtags" : [
                {
                    "text" : "SafeCityLondon",
                    "indices" : [
                        68,
                        83
                    ]
                },
                {
                    "text" : "ldnont",
                    "indices" : [
                        84,
                        91
                    ]
                }
            ],
            "urls" : [
                {
                    "url" : "dfgdf.co",
                    "expanded_url" : "url.ckdkd",
                    "display_url" : "ghjghj",
                    "indices" : [
                        44,
                        67
                    ]
                }
            ],
            "user_mentions" : [ ],
            "symbols" : [ ]
        },
        "favorited" : false,
        "retweeted" : false,
        "possibly_sensitive" : false,
        "filter_level" : "low",
        "lang" : "en"
    }
}

0 个答案:

没有答案