I need to maintain an application where data stored in MongoDB, and I'm fluent yet with the mapReduce syntax.
I want to make a mapReduce operation where the emitted key is the ID of a DBRef, not it's value. I clearly understand from MongoDB documentation that for parallelism reason one mustn't and can't access the DB.
So my collection documents are like that:
{
_id: "66f072b8-2422-4022-826b-b20b832a1ee6",
_class: "com.foo.bar",
foo_bar: 1,
user: {
"$dbref": {
namespace: "user",
oid: "6cd0dac5-a511-48b1-b437-318ad74061a5"
}
}
}
The current mapReduce is like that:
db.myCollection.mapReduce(
function () {
if (this.foo_bar > 0) {
emit(this.user, this.foo_bar);
}
},
function (key, values) {
var sum = 0;
for (var i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
},
{
"out" : { "inline" : 1}
}
)
There is a lot of documents and the loading between AWS and MongoHQ is taking a lot of time. I would like to be able to use user$id as a key, instead of the DBRef's document.
I've tested with the following but without success:
- emit(this.user.id, this.foo_bar);
- emit(this.user.oid, this.foo_bar);
- emit(this.user._id, this.foo_bar);
- emit(this.user.dbref.id,this.foo_bar);
- emit(this.user.dbref.oid, this.foo_bar);
- emit(this.user.dbref._id, this.foo_bar);
- emit(this.user['$dbref'].oid, this.foo_bar); (as per Sammaye suggestion in comments)
What's the correct syntax?
this.user['$dbref'].oid- Sammaye