2

I have the following document, and I have to update isRead of a particular UserId. I will have to search the database based on StoryId, so I have made it as a secondary index.

{
    "DateCreated": "2018-03-07 11:53:33" ,
    "StoryId": "8744d61e-fe8c-41eb-ae51-556ea47a217f" ,
    "Users": [
        {
            "UserId": "8ee4ba69-c284-4e18-badc-9f9e164fe020" ,
            "isRead": "false"
        } ,
        {
            "UserId": "b286914e-0b26-4d7f-baa2-7d638e0bb0c8" ,
            "isRead": "false"
        }
    ] ,
    "id": "a959daac-4399-4feb-af91-951f84a6bc32"
}

This is the query I have tried so far :

ReqlExpr expr = r.db("MyDb").table("MyTable").
    getAll(sStoryId).optArg("index", "StoryId").
    update(row -> row.g("Users").map(doc-> r.branch(doc.g("UserId").eq(sUserId),doc.merge(obj), doc)));

where obj = {isRead=true}

and I am getting error :

Inserted value must be an OBJECT (got ARRAY):
[
        {
                "UserId": "8ee4ba69-c284-4e18-badc-9f9e164fe020",
                "isRead": "false"
        },
        {
                "UserId": "b286914e-0b26-4d7f-baa2-7d638e0bb0c8" ,
                "isRead": "true"
        }
]

does anybody know how to fix this?

I am looking for a solution in java.

(using javascript I am able to update the same like this ):

r.db('MyDb').table('MyTable').get("9b46f1ae-86ce-46b0-b502-e2831f2dced9")
  .update(function (row) {
    return {
      Users: row('Users').filter(function (client) {
        return client('UserId').ne('b286914e-0b26-4d7f-baa2-7d638e0bb0c8')
      })
      .append({ UserId: 'b286914e-0b26-4d7f-baa2-7d638e0bb0c8', isRead: 'true' })
    };
  });

2 Answers 2

1

I cannot leave a comment as i do not have enough reputation..

I suppose you are giving an array to your query? You could transform that to an HashMap and test if it helps

Every documentation i read on the RethinkDB site regarding java uses HashMaps everywhere to pass and receive data.

Sign up to request clarification or add additional context in comments.

Comments

1

Sorry I didn't post my answer earlier.

This is what I ended up doing. I am not so sure if this is an efficient way to do the update

expr = r.db("MyDB").table("MyTable").
    getAll(sStoryId).optArg("index", "StoryId").
    update(row -> r.hashMap("Users", row.g("Users").filter(col -> col.g("UserId").ne(sUserId)).append(obj)));

basically this is what I am doing, I'm filtering all the {UserId, isRead} objects which are not equal to the UserId I'm looking for, then append a new object to the above group. This filtering and appending is done on a hashMap.

I have constructed new object from a json string which looks like this :

{"b286914e-0b26-4d7f-baa2-7d638e0bb0c8": "true"} where b286914e-0b26-4d7f-baa2-7d638e0bb0c8 is the UserId I'm looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.