몽구스를 통해 mapReduce 함수를 사용하려고하는데 전달중인 맵 함수가 호출되지 않습니다. 코드 여기몽구스와 mapReduce - 맵 함수가 호출되지 않았습니다.
[ { data: 'Tag test data',
name: 'Tag Test',
_id: 5130dff2560105c235000002,
__v: 0,
comments: [],
tags: [ 'tag1', 'tag2', 'tag3' ] },
{ data: 'Testing tags. Again.',
name: 'Another test post',
_id: 5131213b611fe1f443000002,
__v: 0,
comments: [],
tags: [ 'tags', 'test', 'again' ] } ]
됩니다 :
var Schema = mongoose.Schema;
var PostSchema = new Schema ({
name : String
, data : String
, tags : [String]
});
mongoose.model('Post', PostSchema);
var o = {};
o.map = function() {
if (!this.tags) {
//console.log('No tags found for Post ' + this.name);
return;
}
for (index in this.tags) {
emit(this.tags[index], 1);
}
}
o.reduce = function(previous, current) {
var count = 0;
for (index in current) {
count += current[index];
}
return count;
}
o.out = { replace : 'tags'}
o.verbose = true;
var Post = mongoose.model('Post');
Post.mapReduce(o, function(error, model, stats) {
console.log('model: ' + model);
console.log('stats: ' + stats);
});
은 "모델"와 객체는 항상 정의되지 않습니다 "통계"및 로그 문 다음은 현재 "포스트"모델 모음에 포함 된 데이터입니다 지도 함수에서 호출되지 않습니다.
Post.find().exec(function(err, posts) {
console.log(posts);
});
어떤 제안 : 나는 맵리 듀스 기능의 외부 포스트 모델과 같은 것을 할 경우 예상대로, 나는 게시물의 상단에있는 데이터를 얻을? 뭔가 약간 벗어난 것이 확실합니다 ...
Mongo의 자바 스크립트 엔진에서 지원하지 않는 맵에서 'console.log'를 호출하고 함수를 줄일 수 없습니다. – JohnnyHK
console.log 문의 출력은 "model : undefined"입니다. 또한 정의되지 않은 오류가 발생하므로 일반 mongo 함수로 모델 객체를 조작 할 수 없습니다. – Geoff
mapReduce 호출에 잘못된 형식을 전달할 수 있습니다. {mapReduce : 'collection', map : < >, reduce : < > 등이 있어야하는 단일 객체로 db.runCommand()를 호출 할 수 있습니다. 모든 도우미가 기대합니다 (지도, 축소, {다른 옵션}) - 해당 구문을 사용해 볼 수 있습니까? –