Is there a way to create projection without explicit listing of document properties?

133 views Asked by At

I have one collection for users, one for user's calendars, (users:calendars have 1:1 relation, but not all user owns a calendar):

users: {"name":"John","role":admin [more fields]}
calendars: {"color":"blue",owner: "users/john"}

I've to return a document with:

{"name":"John","role":admin,[all fields from the users collection],calendar:cal}

Is there a way to do this without listing the properties from the user document?

for cal in zcalendar
    for user in zuser filter user._id == cal.owner
return ...
1

There are 1 answers

3
yojimbo87 On BEST ANSWER

You can use MERGE document function, e.g. like this:

FOR cal IN zcalendar
    FOR user IN zuser
        FILTER user._id == cal.owner
        LET calDoc = {
            'calendar': cal
        }
        RETURN MERGE(user, calDoc)

Which returns this structure:

[
  {
    "_id": "...",
    "_rev": "...",
    "_key": "...",
    "name": "John",
    "role": "admin",
    "calendar": {
      "_id": "...",
      "_rev": "...",
      "_key": "...",
      "color": "blue",
      "owner": "..."
    }
  }
]