How can I retrieve the id of a document I added to a Cosmosdb collection?

Related searches

I have a single collection into which I am inserting documents of different types. I use the type parameter to distinguish between different datatypes in the collection. When I am inserting a document, I have created an Id field for every document, but Cosmosdb has a built-in id field.

How can I insert a new document and retrieve the id of the created Document all in one query?

I think you just need to .getResource() method to get the create document obj.

Please refer to the java code:

DocumentClient documentClient = new DocumentClient(END_POINT,
                    MASTER_KEY, ConnectionPolicy.GetDefault(),
                    ConsistencyLevel.Session);

Document document = new Document();
document.set("name","aaa");

document = documentClient.createDocument("dbs/db/colls/coll",document,null,false).getResource();

System.out.println(document.toString());

//then do your business logic with the document.....

C# code:

Parent p = new Parent
            {
                FamilyName = "Andersen.1",
                FirstName = "Andersen",
            };

Document doc = client.CreateDocumentAsync("dbs/db/colls/coll",p,null).Result.Resource;
Console.WriteLine(doc);

Hope it helps you.

Get a Document, Gets or sets the Id of the resource in the Azure Cosmos DB service. As documents are added to a collection, Cosmos DB by default automatically indexes the documents, thus allowing documents to be queried. It is at the collection level that you configure the indexing policy. Since indexing policy is set at the collection level, each collection within a database can have different indexing policies.

The CreateDocumentAsync method returns the created document so you should be able to get the document id.

 Document created = await client.CreateDocumentAsync(collectionLink, order);

Resource.Id Property (Microsoft.Azure.Documents), id, It is the unique name that identifies the new collection. can inspect the conflicting resources by performing a GET on the conflicts URI path. As documents are added to a collection, Cosmos DB by default automatically´┐Ż List (ReadFeed) Documents. 03/29/2016; 4 minutes to read; In this article. Performing a GET on the documents resource of a particular collection, i.e. the docs URI path, returns a list of documents under the collection. ReadFeed can be used to retrieve all documents, or just the incremental changes to documents within the collection. Request

Sure, you could always fetch the id from creation method response in your favorite API as already shown in other answers. You may have reasons why you want to delegate key-assigning to DocumentDB, but to be frank, I don't see any good ones.

If inserted document would have no id set DocumentDB would generate a GUID for you. There wouldn't be any notable difference compared to simply generating a new GUID yourself and assign it into id-field before save. Self-assigning the identity would let you simplify your code a bit and also let you use the identity not only after persisting but also BEFORE. Which could simplify a lot of scenarios you may have or run into in future.

Also, note that you don't have to use GUIDs as as id and could use any unique value you already have. Since you mentioned you have and Id field (which by name, I assume to be a primary key) then you should consider reusing this instead introducing another set of keys.

Self-assigned non-Guid key is usually a better choice since it can be designed to match your data and application needs better than a GUID. For example, in addition to being just unique, it may also be a natural key, narrower, human-readable, ordered, etc.

Collections, Azure Cosmos DB is a globally distributed multi-model database that Aside from the required ID property, users can define any arbitrary You can insert, replace, delete, read, enumerate, and query arbitrary JSON documents in a collection. User access to documents can be managed via permissions. id: Required: String: It is the unique ID that identifies the document, that is, no two documents should share the same id. The id must not exceed 255 characters. The ID field is automatically added when a document is created without specifying the ID value. However, you can always update the ID value by assigning a custom value to it in the

Documents, Represents a document collection in the Azure Cosmos DB service. The example below queries for a Database by Id to retrieve the SelfLink. Copy. AltLink: Gets the alt-link associated with the resource from the Azure Cosmos DB service. (Inherited from Resource) : ConflictResolutionPolicy: Gets or sets the ConflictResolutionPolicy that is used for resolving conflicting writes on documents in different regions, in a collection in the Azure Cosmos DB service.

DocumentCollection Class (Microsoft.Azure.Documents), # Get the Record Count in Cosmos DB When working with Azure Cosmos DB, it is guaranteed that at some point that you'll need to get the record count of a document. There are a couple of quick ways of how to do this through the Azure Portal by navigating to the Cosmos DB resource you wish to query and selecting the Data Explorer tab and using the

Headers. See Common Azure Cosmos DB REST request headers for headers that are used by all Cosmos DB requests.. Body. None. Response. Get Collection returns the body of the collection as persisted in Cosmos DB along with response headers that report the quota and usage of the collection.

Comments
  • Not sure what you're want.If you don't set an id field , cosmos db will generate it automaticlly for you. Do you mean you want to retrieve document by this auto-generate id field?
  • Yes. I want to let cosmosdb generate the id for me, but I want to read it immediately and use it to perform further operations.
  • CreateDocumentAsync returns a Task<ResourceResponse<Document>> not a Task<Document>, so I don't think this will work.
  • Personally I don't believe that any natural keys that meet the uniqueness and stability requirements of a key exist in the real world.