CosmosDB Join (SQL API) - azure

I'm using CosmosDB using SQL API and I'm trying to join two collections. I saw join example within a document but not getting what actually looking.
RequestLog
{
"DateTimeStamp": "2018-03-16T10:56:52.1411006Z",
"RequestId": "8ce80648-66e2-4357-98a8-7a71e8b65301",
"IPAddress": "0.0.0.173"
}
ResponseLog
{
"DateTimeStamp": "2018-03-16T10:56:52.1411006Z",
"RequestId": "8ce80648-66e2-4357-98a8-7a71e8b65301",
"Body": "Hello"
}
Is it possible to join both collections? how?

Actually Cosmos DB JOIN operation is limited to the scope of a single document. What possible is you can join parent object with child objects under same document.
Cross-document joins are NOT supported, so you would have to implement such query yourself.

It is not possible to write join queries across multiple collections in Cosmos, or even across multiple documents in a single collection for that matter. Your only options here would be to issue separate queries (preferably in parallel) OR if your documents lived together in the same collection, you could retrieve all the relevant logs for a request using the common RequestId property.
SELECT * from c WHERE c.RequestId = '8ce80648-66e2-4357-98a8-7a71e8b65301'
This will only work if the object structure across the documents is the same. In this example it's possible because they both share a property of the same name called RequestId. You can't do JOIN on arbitrary properties.

In Azure Cosmos DB, joins are scoped to a single item. Cross-item and cross-container joins are not supported. check documentation here

Related

How to do inner join with two containers in Cosmos DB

I have two containers in Cosmos DB and I need to do an inner join between them.
Exemple:
VD_VENDAS
VD_ESTOQUE
VD_COMPRAS
I need to do inner join between VD_VENDAS and VD_ESTOQUE. I try it:
SELECT v.obraId
FROM VD_VENDAS v
JOIN VD_ESTOQUE e IN v.obraId
But it doesn't work. How can I do it ?
Cosmos DB's JOIN is only for self-joins (joining against data within the same document). There is no way to run a query that performs a join across multiple containers.
To accomplish something similar to what you want, you'll need to run two queries: one against each container (with whatever filtering you need to do). And how you write those queries is really up to you.

Is it possible to use multiple databases within same query using pg-promise?

I have two related tables T1 and T2 in different databases D1 and D2. I need to do an inner join between two tables.
From here: Joining Results from Two Separate Databases it is clear that separate queries should be made to two databases and results to be consolidated on client side OR use dblink / postgres-fdw.
However, I see this issue: Multiple Databases #1
and the use of $dc parameter here: pg-promise/Database.
I believe issue: Multiple Databases #1
just facilitates allowing connecting to multiple databases within the same codebase.
The description of $dc parameter states:
This is mainly to facilitate the use of multiple databases which may need separate protocol extensions, or different implementations within a single task
However, I did not find any examples.
Is the $dc paramter just a database context object that can be accessed, or would it allow to do an inner join between two different databases?
Is there a way to do utilise two database connections but do a join across databases in without having to do it on client-side using pg-promise?
Is the $dc paramter just a database context object that can be accessed, or would it allow to do an inner join between two different databases?
It is the former.
Is there a way to do utilise two database connections but do a join across databases in without having to do it on client-side using pg-promise?
No. Each Database object represents only a single connection to A database.
Database Context is there to allow re-use of tasks, transactions and protocol extensions across multiple Database objects, by relying on its value.

Are MongoDB queries client-side operations?

Lets say I have a document
{ "_id" : ObjectId("544946347db27ca99e20a95f"), "nameArray": [{"id":1 , first_name: "foo"}]
Now i need to push a array into nameArray using $push . How does document update in that case. Does document get's retrieved on client and updates happens on client and changes are then reflected to Mongodb database server. Entire operation is carried out in Mongodb Database.
What you are asking here is if MongoDB operations are client-side operations. The short answer is NO.
In MongoDB a query targets a specific collection of documents as mentioned in the documentation and a collection is a group of MongoDB documents which exists within a single database. Collections are simply what tables are in RDBMS. So if query targets a specific collection then it means their are perform on database level, thus server-side. The same thing applies for data modification and aggregation operations.
Sometimes, your operations may involve a client-side processing because MongoDB doesn't provides a way to achieve what you want out of the box. Generally speaking, you only those type of processing when you want to modify your documents structure in the collection or change your fields' type. In such situation, you will need to retrieve your documents, perform your modification using bulk operations.
See the documentation:
Your array is inserted into the existing array as one element. If the array does not exists it is created. If the target is not an array the operation fails.
There is nothing stated like "retriving the element to the client and update it there". So the operation is completely done on the database server side. I don't know any operation that works in the way like you described it. Unless you are chaining a query, with a modify of the item in your client and an update. But these are two separated operations and not one single command.

DynamoDB: Store array of IDs and do a batchGet for each ID

In DynamoDB, I have a Groups table and a Users table. One or many Users can belong to a Group.
Using DynamoDB, is it possible to perform one query to get a single Group by ID, and also all of the Users in that Group by the User IDs in that Group record?
If not, what is the most efficient way to do this?
No, you cannot do JOINs in NoSQL databases. The way you can do is to retrieve your group. read all the userIds. And then use either batchGet or query/scan(if its primary index) using "IN" operator

MongoDB merge one field into existing collection with Map/Reduce

I have a MongoDB database with 2 collections:
groups: { group_slug, members }
users: { id, display name, groups }
All changes to groups are done by changing the members array of the group to include the users ids.
I want to sync these changes across to the users collection by using map/reduce. How can I output the results of map/reduce into an existing collection (but not merging or reducing).
My existing code is here: https://gist.github.com/morgante/5430907
How can I output the results of map/reduce into an existing collection
You really can't do it this way. Nor is this really suggested behaviour. There are other solutions:
Solution #1:
Output the map / reduce into a temporary collection
Run a follow-up task that updates the primary data store from the temporary collection
Clean-up the temporary collection
Honestly, this is a safe way to do this. You can implement some basic retry logic in the whole loop.
Solution #2:
Put the change on a Queue. (i.e. "user subscribes to group")
Update both tables from separates workers that are listening for such events on the queue.
This solution may require a separate piece (the queue), but any large system is going to have such denormalization problems. So this will not be the only place you see this.

Resources