简体   繁体   中英

How to append to possibly non-existent subdocument in RethinkDB?

I have a JSON schema that validates documents like this:

{
    "id": "abc123",
    ...
    "galleries": {
        <category>: {
            "items": [],
            "visible": true
        }
    }
}

The "galleries" part is not required by the validation (some documents may not have a gallery) and the <category> part is simply an arbitrary string, for eg. "screenshots".

How can I append an image to the gallery items if neither "galleries" nor <category> are guaranteed to exist? Let's say I want to append an image to the "screenshots" gallery. Do I have to make two queries and check for existence first or is it possible to do this atomically?

EDIT: My current solution is I query for a game's galleries field with .default({}) then process the resulting dict with Python and do an update with merge.

你可以这样做:

r.table('tbl').get(abc123).update({galleries: {<category>: {items: r.row('galleries')(<category>)('items').default([]).append(NEW_ITEM)}}})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM