Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs mongoose mapReduce如何使用外部数据 #61

Open
iliuyt opened this issue May 6, 2020 · 0 comments
Open

nodejs mongoose mapReduce如何使用外部数据 #61

iliuyt opened this issue May 6, 2020 · 0 comments

Comments

@iliuyt
Copy link
Owner

iliuyt commented May 6, 2020

默认情况下mapreduce是无法使用外部变量的,当map或reduce等方法使用外部数据执行时,会报错找不到对象。
image

根据mongoose文档中可以看到scope参数,文档告知在执行期间暴露给map / reduce / finalize的{Object}作用域变量文档地址

var opt = {};
opt.scope = {
  userData: {
    position: 2,
    brand: 2
  }
};

opt.map = function() {
    emit("error", userData.position);
};

opt.reduce = function(k, vals) {
  return vals.join(",");
};
opt.out = { replace: "post_total" };
Role.mapReduce(opt, function(err, model) {
  if (err) {
    console.log(err);
  } else {
    model.model.find().exec(function(err, docs) {
      console.log(docs);
    });
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant