Postman: Cannot Create Document with Azure MongoDB API - azure

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.

Related

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?

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.

DocumentDB: Bulk-Import Stored Procedure: Insert multiple partition key documents in COSMOS DB

I am working on Bulk insert stored procedure in Cosmos database using document client but the challenge that I am facing is, I need to insert documents in bulk that may have different partition keys.
Is there any way to achieve it?
I am currently using the below code:
Uri uri = UriFactory.CreateStoredProcedureUri("test-db", "test-collection", "sp_bulk_insert");
RequestOptions options = new RequestOptions { PartitionKey = new PartitionKey("patient")};
var result = await _client.ExecuteStoredProcedureAsync<string>(uri, options , patientInfo, pageInfo);
return result;
But I also have pageInfo object having partition key: "page" but given PartitionKey in RequestOptions is "patient" that is the partition key of patientInfo object
When I am trying to execute the SP it is giving following error:
Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted
Stored procedures are scoped to a single partition key so this not possible. Also there is no reason to use stored procedures for bulk operations. You are better off using the .NET SDK v3 and leveraging the bulk support in there. https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/BulkSupport
Thanks.

Get dynamodb items where a nested key exist

I would like to return items where a nested key exists. I have the following table:
"users": [
{
"active": true,
"apps": {
"app-name-1": {
"active": true,
"group": "aaaaaaaaa",
"settings": {}
}
},
"username: "user1"
},
{
"active": true,
"apps": {
"app-name-2": {
"active": true,
"group": "bbbbbb",
"settings": {}
}
},
"username: "user2"
]
So I want to return all users that have "app-name-1" under "apps". Which operation is the best for this purpose?
The question you need to ask yourself isn't just the "operation", but also how do you model your data in DynamoDB. I.e., how does that JSON array you showed translates into a DynamoDB table, with hash and sort keys?
While DynamoDB nominally does supports nested attributes, this support is actually only partial, with some features (notably secondary indexes) not supporting them, so as I'll show now it is better not to use them. To model your data without nested attributes, what you can do is to use a hash key "username" and sort key "appname". Each item in this table is one app belonging to one user. The user's "active" flag is a bit of a problem in this modeling, but you can implement it by using a fake appname for storing such user parameters.
This modeling makes it efficient to list all applications belonging to one user (I assume you need this feature as well...) but not all users with a certain application. But you were looking for the reverse operation - to get a list of users given an app name.
You can get this reverse with a Scan operation but this is a full-table scan, and accordingly can be very slow and expensive (you'll be paying to read the entire database, even if only part of the data is actually returned to the user).
If efficient search by app is important, you should create a secondary index (GSI) whose hash key is app-name and sort key is user (i.e., the opposite key order from that of the base table). You can then query this index to get - efficiently - the list of usernames that have this app.
Note that such a GSI wouldn't have been possible if you were to insist of modeling your "user" item with nested attributes, because GSIs don't support nested attributes as the key.

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