0

I have a following structure of document (omiting all with underscore prefix like _self)

{
    "id": "c5055e2b-efb2-4c86-907d-a0beb1dca4dc",
    "Name": "John Johnson",
    "partitionKey": "0ecdb989-01c6-4f11-9fd2-3e1dcc1c8cb9",
    "FKToBeDeleted": "FK_c5055e2b-efb2-4c86-907d-a0beb1dca4dc_ToBeDeleted",
}

And as You can see there is a field named FKToBeDeleted and I use this to mark document, but it has to be as a reference, because in my app may occure kind of database concurrency, because 1st app can GET document, process it, and 2nd app can update document during processing and 1st one will not see any changes, because downloading again huge document and updating it is RU consuming, so I wanted to reduce the cost. Going further, I created a document for this.

{
    "id": "FK_c5055e2b-efb2-4c86-907d-a0beb1dca4dc_ToBeDeleted",
    "partitionKey": "0ecdb989-01c6-4f11-9fd2-3e1dcc1c8cb9",
    "ToBeDeleted": false,
}

And now there is a problem, because my front-end app does not want to display any ToBeDeleted documents. This kinda cheats the user, because I just mark it as deleted but later delete the document.

Now the question is how the SQL query should look like? Previously it was like the following query, because r.ToBeDeleted was boolean.

SELECT r.id, r.Name, r.AddedAt, r._ts
FROM ROOT r 
WHERE 
(NOT(r.ToBeDeleted)) 
ORDER BY r.AddedAt desc 

Now FKToBeDeleted is only a reference to another document, but the ID is in r.FKToBeDeleted, so I tried some nested SELECT but it didn't work.

Any suggestions what is the right way to achieve that?

EDIT (clarification)

enter image description here

Let's have a following situation. There are two apps (you can also treat them as threads) which uses the same Cosmos DB instance.

STEP 1 - is a moment of start of processing some data, but database document is needed, so it gets that and on the right side you can see current document (but in fact only ToBeDeleted is interesting here).

STEP 2 - is a moment, when user wants to remove this processed item, because he is no longer interested of its results, but database document is also required here, so again there is a GET.

STEP 3 - is a moment, when job of soft delete is done and there is a need to update database document, and the field is set to true.

STEP 4 - is a moment, when processing is over of common flow and at the end there is update of the document. BUT, Application 2 downloaded it before STEP 3, and it's overriding things that Application 1 did, which is bad.

So I made a solution for that.

enter image description here

As you can see, the steps are the same, but instead of updating the same document, I update a referenced document, so I don't have a problem with overriding data.

Now, the problem is how to make a SQL query to join two documents, so the FK_1 id will be replaced of the value of ToBeDeleted field in another document.

4
  • Why would you write to another container a record that says to delete an item in another container? This seems way to complicated. Why not just put a soft delete flag into the first container with the record and simply filter for that?
    – Mark Brown
    Jun 5, 2020 at 17:58
  • @MarkBrown I made an edit for you. Perhaps I wasn't clear. The main thing is to do some sql query that can join two documents in the same collection.
    – Vrangz
    Jun 6, 2020 at 13:53
  • Why don't you just delete the document instead of setting the flag. Then if another process attempts to do an update they will receive a 404. Or just use the if-match for updates and if you get a 412 then fail the update. If it's in your delete() function just fail and if an update, refetch the document and handle however you resolve conflicts.
    – Mark Brown
    Jun 7, 2020 at 18:29
  • I do not delete the document immediately, because I will get a 404 and when I get a 404 I notify, that something went wrong which in fact was intended and shouldn't be treated as an error. But this is much complex than just updating a document. I also store files, download them and I do many things which can go wrong when the document is deleted during processing. I'm aware of some other solutions, but making a JOIN for two documents seems the easiest, yet I don't know if possible.
    – Vrangz
    Jun 8, 2020 at 6:19

2 Answers 2

0

According to this article there is no possibility to join two documents, which of course does not help me at all, yet closes the topic.

JOIN keyword exists in the language, but it is used to “unfold” nested containers, there is no way to join different documents.

0

Perhaps, you can use subquery instead of JOIN.

https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-subquery#mimic-join-with-external-reference-data

1
  • This is more appropriate in comments rather than in answers
    – shuberman
    Jul 1, 2020 at 6:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.