I'm working on some changes for the
officeradar demo app and trying to show a list of profiles which I want to have reverse sorted by how recent they've had a geofence event for that profile. Eg:
[Traun entered SF 5 hours ago]
[Zack entered SF 8 hours ago]
[Hideki entered MV 2 days ago]
The document model is as follows:
Profile doc
{
"_id":"tleyden",
"lastGeofenceEvent":"doc-234324232"
}
GeofenceEvent doc
{
"id":"doc-234324232",
"createdAt":"2014-10-24",
"beaconLocation":"sf"
}
As I was writing the map function to generate the UI mentioned above, I wanted to emit the createdAt field, but realized that would require a doc lookup. Eg:
Mapper map = new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if (document.get("type").equals("profile")) {
// I have a profile doc, but I only have the id of the
// latest geofence event associated with this profile,
// and I'd need to do a db lookup to get it, which
// would violate the rules of a map function .. so I'm stuck
}
}
};
Proposed solution
I figured I could add a new field to the profile doc to hold the date of the latest geofence event associated with this profile (denormalize + duplicate):
Profile doc (updated):
{
"_id":"tleyden",
"lastGeofenceEvent":"doc-234324232",
"lastGeofenceEventCreatedAt":"2014-10-24"
}
The map function would become:
if (document.get("type").equals("profile")) {
emit(document.get("lastGeofenceEventCreatedAt"), ...);
}
and the problem would be solved.
But, I'm wondering is this the best practice or is there a better way? @Jens I remember in your advanced couchbase lite talk you mentioned "poor man's joins", but I didn't catch the details. Would that apply here?