How to get document from cosmos db using SQL Rest API when the collection has partition key , but the partition key is empty for document? - azure

I have tried with value as "null" and "Undefined.Value" but no success . I am using SQL REST Api for Cosmos and the collection has partition key that makes it necessary to add the header "x-ms-documentdb-partitionkey" header with value of the key .
I am currently testing from postman

As per my tests, I created a CosmosDB with a partition of /pk.
I created 2 documents.
"id": "0001",
"name": "Test1",
"pk": ""
"id": "0002",
"name": "Test2"
My Rest API call was able to pick up id "0001" with a partition key of [""] but it was not able to pick up id = "0002" with the same partition key. This tells me that the 2nd scenario is where you are at. The items were written without mentioning the partition key and in such a case, you will not be able to fetch the data using the rest API. If you set the partition key as blank while inserting the document, you should be able to retrieve the documents.
In short setting "pk" to "" is not the same as not setting it at all.

Related

Why does my Azure Cosmos DB SQL API Container Refuse Multiple Items With Same Partition Key Value?

In Azure Cosmos DB (SQL API) I've created a container whose "partition key" is set to /part_key and I am now trying to create and edit data in Data Explorer.
I created an item that looks like this:
{
"id": "test_id",
"value": "val000",
"magicNumber": 32,
"part_key": "asdf"
}
I am now trying to create an item that looks like this:
{
"id": "frank",
"value": "val001",
"magicNumber": 33,
"part_key": "asdf"
}
Based on the documentation I believe that each item within a partition key needs a distinct id, which to me implies that multiple items can in fact share a partition key, which makes a lot of sense.
However, I get an error when I try to save this second item:
{"code":409,"body":{"code":"Conflict","message":"Entity with the specified id already exists in the system...
I see that if I change the value of part_key to something else (say asdf2), then I can save this new item.
Either my expectations about this functionality are wrong, or else I'm doing this wrong somehow. What is wrong here?
Your understanding is correct, It could happen if you are trying to instead a new document with id equal to id of the existing document. This is not allowed, so operation fails.
Before you insert the modified copy, you need to assign a new id to it. I tested the scenario and it looks fine. May be try to create a new document and check

Postman: Cannot Create Document with Azure MongoDB API

When creating the document with Azure Cosmos DB: MongoDB API, I am facing the below issues which varies on parameters.
When creating collection UserProfile, I've created it with Partition Key username.
Request1: - Without Partition Key
Headers:
x-ms-documentdb-is-upsert: true
Body:
{"username": "test"}
Issue:
The partition key supplied in x-ms-partitionkey header has fewer
components than defined in the the collection.
Request2: - With Partition Key
Headers:
x-ms-documentdb-is-upsert: true
x-ms-documentdb-partitionkey: ["username"]
Body:
{"username": "test"}
Issue:
One of the specified inputs is invalid
Request3: - With Partition Key & id specified in body
Headers:
x-ms-documentdb-is-upsert: true
x-ms-documentdb-partitionkey: ["username"]
Body:
{"id": "test", "username": "test"}
Issue:
PartitionKey extracted from document doesn't match the one specified
in the header
In any case,
I am not able to create the document. What are the necessary parameters to create the document which has partition specified?
Two points:
If you're using the Mongo API, you should not use the REST API. While it's technically possible, it is not supported to use both together. 100% do not recommend.
partition key needs to be the partition key value, not the path itself, so it would be "test", not "username". It already knows that "username" is the path.

Query from Azure Comos DB and save to Azure Table Storage using Data Factory

I want to save C._ts+C.ttl as one entity in my Azure Table Storage. I do the following query in my Copy Activity:
"typeProperties": {
"source": {
"type": "DocumentDbCollectionSource",
"query": {
"value": "#concat('SELECT (C.ts+C.ttl) FROM C WHERE (C.ttl+C._ts)<= ', string(pipeline().parameters.bufferdays))",
"type": "Expression"
},
"nestingSeparator": "."
},
I dont want to copy all the fields from my source i.e. CosmosDB to my sink i.e. Table Storage. I just want to store the result of this query as one value. How can I do that?
According to my test, I presume the null value you queried because of that the collection level ttl affects each document , but will not generate ttl property within document.
So when you execute SELECT c.ttl,c._ts FROM c , just get below result.
Document level ttl is not defined, just follow collection level ttl.
You need to bulk add ttl property into per document so that you could transfer _ts+ttl caculator results.
Your Copy Activity settings looks good , just add an alias in SQL, or set the name of the field via column mapping.
Hope it helps you.

flask dyanmo query for count fileds

consider this schema in dyanmo db,we count of question
[
{
'TableName': "user_detail",
'KeySchema': [
{'AttributeName': "timestamp", 'KeyType': "HASH"},
{'AttributeName': "question", 'KeyType': "RANGE"},
],
'AttributeDefinitions': [
{'AttributeName': "timestamp", 'AttributeType': "S"},
{'AttributeName': "question", 'AttributeType': "N"},
],
'ProvisionedThroughput': {
'ReadCapacityUnits': 40,
'WriteCapacityUnits': 40] }
}
]
I'm beginner of dyanmo db can any one give idea for that one.we need query,the sql query goes like that select count(question) from user_detail where question =1
Thanks in advance
I will throw some pointers. DynamoDB has two types of APIs :-
Option 1:-
1) Scan API - will scan the whole table. The scan api should be used when the hash key value is not known
2) Query API - will query the table using hash key. The hash key is must for Query API
In your case, the hash key value is not known. So, you can't use Query API. However, you can use scan API which is a very costly operation in terms of performance and cost. So, it should be avoided if you have a table of millions of items.
The alternative is to create global secondary index (GSI) with question attribute as hash key and some other field as sort key (possibly timestamp). This way, you should be able to use Query API on GSI. However, this wouldn't solve the problem completely.
DynamoDB doesn't have aggregate functions like count,min and max. So, you need to count the number of items in the result set at client side.
Option 2:-
If you have an option to change the data model, you can redesign the above table as mentioned below:-
question - hash key
timestamp - range key
I have seen many use cases using timestamp as range key. Please analyse your query access patterns (QAP) for all your use cases and make the decision accordingly.

DocumentDB REST API: PartitionKey extracted from document doesn't match

I'm trying to insert a JSON document into DocumentDB via REST, using PHP (which lacks an official API wrapper). Now, it seems that a partition key has become mandatory for any collection in DocumentDB, but i cannot find the REST API documented.
I get the following error in return:
PartitionKey extracted from document doesn't match the one specified in the header
The JSON document I'm trying to insert looks as follows:
{ id:"1", ... "domain":"domain.com" }
In Azure, I have defined the collection with the following partition key:
/domain
And when sending the REST request, I send along the following header:
x-ms-documentdb-partitionkey: [ "domain" ]
What am I missing here?
For x-ms-documentdb-partitionkey value you would need to specify the partition key value ("domain.com") and not the partition key attribute ("domain").
x-ms-documentdb-partitionkey: [ "domain.com" ]
Once you do this, documents matching this partition key value will be returned.

Resources