TheSoundDefense
TheSoundDefense

Reputation: 6945

Cloudant NoSQL: retrieve all documents associated with key not known at query time

So in our Cloudant database, we store multiple "schedule" documents, and each one has a number of associated "event" documents. Here's a sample schema:

Schedule:

{
 "_id": "123",
 "_rev": "456",
 "type": "schedule",
 "userId": "sampleUser"
}

Event:

{
 "_id": "1234",
 "_rev": "5678",
 "type": "event",
 "scheduleId": "123"
}

If we know the _id of the schedule ahead of time, it's easy enough to get the schedule and associated events in a single query. What I'm wondering is if this is possible to do in a single query if we only know the userId. Is there any way to obtain all schedules associated with a userId, and all events associated with those schedules, in a single query? Or are we stuck doing two queries here? I looked at a handful of join tutorials and they didn't explain how it might be feasible, but I'm not sure it's totally impossible.

If this isn't possible, I can just include an array of all event IDs in the schedule doc, but that's not an ideal solution since it requires us updating the schedule doc every time a new event is added.

Upvotes: 1

Views: 145

Answers (1)

Chris Snow
Chris Snow

Reputation: 24626

You will need two queries unless you are able to change your data model and make Events part of the Schedule document.

Upvotes: 1

Related Questions