1
我處理文件,如以下各項之一:MapReduce的分割輸入串入輸出數組
> db.productData.find({"upc" : "XXX"}).pretty()
{
"_id" : ObjectId("538dfa3d44e19b2bcf590a77"),
"upc" : "XXX",
"productDescription" : "bla foo bar bla bla fooX barY",
"productSize" : "",
"ingredients" : "foo; bar; foo1; bar1.",
"notes" : "bla bla bla"
}
>
- 我想有含有的字段,列表/分裂成分的陣列中的文件(在
;
)。我想將原始集合的字符串拆分爲一個字符串數組。 - 我想只映射輸出集合中的一些輸入字段。
- 我想在MongoDB上使用
mapreduce
。
我已經嘗試了很多不同的方式從map
功能移動的東西到reduce
功能未能找到一個妥善的解決辦法。 從我所進行的嘗試,現在我知道我需要檢查null
值等,所以下面一個是我最後一次嘗試:
地圖功能:
var mapperProductData = function() {
var ingredientsSplitted = values.ingredientsString.split(';');
var objToEmit = {barcode : "", description : "", ingredients : []};
// checking for null (is this strictly necessary? why?)
if (
this.hasOwnProperty('ingredients')
&& this.hasOwnProperty('productDescription')
&& this.hasOwnProperty('upc')
) {
for (var i = 0; i < ingredientsSplitted.length; i++) {
// I want to emit a new document only when I have all the splitted strings inside the array
if (i == ingredientsSplitted.length - 1) {
objToEmit.barcode = this.upc;
objToEmit.description = this.productDescription;
objToEmit.ingredients = ingredientsSplitted;
emit(this.upc, objToEmit);
}
}
}
};
的減少功能:
var reducerNewMongoCollection = function(key, values) {
return values;
};
的地圖,減少電話:
db.productData.mapReduce(
mapperProductData,
reducerNewMongoCollection,
{
out : "newMongoCollection" ,
query: { "values" : {$exists: true} }
}
);
我在輸出中獲得一個空集合(newMongoCollection
爲空)。 我在做什麼錯?