简体   繁体   中英

Retrieving latest inserted _id(ObjectId) in azure cosmos db(mongo db API)

I would like to know if we can retrieve latest inserted _id(ObjectId) created by CosmosDb(mongoDb) on the same connection. (similar to SCOPE_IDENTITY() in sql server). Im inserting the document from azure functions using CosmosDb output binding.

Per my knowledge, there is no similar function like SCOPE_IDENTITY() of SQL Server in MongoDb API.

We can get the latest document via sorting Azure Cosmos DB's internal Timestamp (_ts) property, that is a number representing the number of elapsed seconds since January 1, 1970.

The query will be like:

db.YourCollection.find().sort({"_ts":1}]).limit(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM