我在我的两个系列中运行以下命名,名为“玩家”和“药房”。
我想加入他们的objectId ObjectId("1287943s76a87flkl2")
和//Merge data so it outputs a complete row of data from the reduce function, no rows should appear if they don't have all the data
player_map = function() {
emit(this.pharmacy, {"firstname" : this.first_name, "lastname" : this.last_name, "email" : this.email});
}
pharma_map = function() {
emit(this._id, {"name" : this.name, "country" : this.country});
}
r = function(key, values) {
var result = {
"firstname" : "",
"lastname" : "",
"email" : "",
"name" : "",
"country" : ""
};
values.forEach(function(value) {
if(value.firstname !== null) {result.firstname = value.firstname;}
if(value.lastname !== null) {result.lastname = value.lastname;}
if(value.email !== null) {result.email = value.email;}
if(value.name !== null) {result.name = value.name;}
if(value.country !== null) {result.country = value.country;}
});
return result;
}
res = db.players.mapReduce(player_map, r, {out: {reduce : 'joined'}});
res = db.pharmacies.mapReduce(pharma_map, r, {out: {reduce : 'joined'}});
,例如{
__v: 3,
_id: ObjectId("552496ea6215d31100fa1d984"),
activation_code: 5633,
country: "AU",
createdAt: ISODate("2015-04-08T02:48:10.491Z"),
email: "me@example.com",
first_name: "John",
password: "57467845742675427",
pharmacy: ObjectId("5521d924847351c8243256")
}
。
我遇到的问题是,当我运行mapReduce函数时,第一个将它的行输出到“join”集合,第二个将它的行输出到“join”集合。
我希望在新的“已加入”集合中看到的是它已找到连接的数据。目前它将所有数据放入我不想要的两个集合中 - 只想要具有完整数据的行。
{
name: "Test Pharmacy",
_id: ObjectId("5521d924847351c8243256"),
products: [],
country: "AU",
createdAt: ISODate("2015-04-06T00:53:56.166Z"),
__v: 0
}
玩家文件
db.players.find().forEach(function(pl) {
pharmacies=db.pharmacies.find({_id:pl.pharmacy});
pl.pharmacy_name=pharmacies.name;
pl.pharmacy_country=pharmacies.country;
db.merged.insert(pl);
});
药房文件
conf/slaves
我也尝试了这个,虽然它在新的合并集合中插入了country和name字段,但它并没有插入2个字段的数据。
spark-slave1.com
spark-slave2.com
localhost