How to obtain _ts in CosmosDB? - azure-cosmosdb

I have a Posts collection on CosmosDB and each post needs to have an insertion date. Since cosmosDB already inserts the _ts field on the documents, I thought that might use that value instead of inserting my own timestamp manually. However, when I try to put an object (in Java) into the collection I get the following error: 400 Unrecognized field "_ts" (class scc.models.User), not marked as ignorable.
Is there a way to obtain _ts in the objects (I'm not talking about a query to obtain _ts of a specific object but instead for _ts be one of the variables of the object when I retrieve it from CosmosDB).

You could just add a property
#JsonProperty("_ts") private String timeStamp; on your User model. If you give it a value when updating (marshalling) it seems to be ignored, but for transparency you could add #JsonIgnore on the timeStamp field as well. If you want to do more low level adjustments you could also provide your own version of the cosmosdbObjectMapper bean (see AbstractCosmosConfiguration).

I am using the C# SDK (I know you are Java - but maybe it's analogous.)
After retrieving a document, I can access the system property _ts as a DateTime via the property Resource.TimeStamp (within namespace Microsoft.Azure.Documents.)

Related

CosmosDB create compound unique index error

I wanted to create a compound unique index using 2 fields, but one of those has a path to some inner field like evaluated.rut. But I got an error:
I don't get the error, in MongoDB I had created index like that, but I can't now in CosmosDB, any suggestion?
Provided the nested path is not an array, the support for this is coming out very soon using the value EnableUniqueCompoundNestedDocs in the capabilities property array for a Cosmos DB databaseAccounts resource in ARM.
This is not yet publicly available. If you'd like to try today you can open a support ticket and ask this capability be added your Cosmos account. You can also DM me on Twitter (see my SO profile) and send me your Cosmos account name and I can request it for you.
You can refer to this document for what is not supported and its limitations.

How to correctly define ID and DateTime fields in Firebase Firestore database?

I am using Firebase FireStore database for the first time and I have the following question.
I have created a calendar collection. This collection will contains document representing events that have to be shown into a Calendar implemented by an Angular application.
So I am defining the following fields for these documents:
id: int. It is a unique identifier of the specific document\event.
title: string. It is the event title.
start_date_time: string. It specifies the date and the time at which the event starts.
end_date_time: string. It specifies the date and the time at which the event ends.
And here I have some doubts:
Is the id field required? From what I know I will have the document UID that will ensure the uniqueness of the document. If not strongly required adopt an id field can be convenient have something like an auto increment field? (I know that I have to handle in some other way the auto increment because FireStore is not a relational DB and doesn't automatically handle it). For example I was thinking that can be useful to order my document from the first inserted one to the last inserted one.
It seems to me that FireStore doesn't handle DateTime field (as done for example by a traditional relational database). Is this assumption correct? How can I correctly handle my start_date_time and end_date_time fields? These field have to contains the date time used by my Angular application? So for example I was thinking that I can define it as string field and put into these fields values as 2020-07-20T07:00:00 representing a specific date and a specific time. It could be considered a valid approach to the problem or not?
Is the id field required?
No fields are required. Firestore is schema-less. The only thing a document requires is a string ID that is unique to the collection where it lives.
There is no autoincrement of IDs. That doesn't scale massively the way Firestore requires. If you need ordering, you will have to define that for yourself according to your needs.
In general, you are supposed to accept the randomly generated IDs that the Firebase client APIs will generate for you. Ordering is typically defined using a field in the document.
It seems to me that FireStore doesn't handle DateTime field
Firestore has a timestamp field type that stores moments in time to nanosecond precision. There is no need to store a formatted string, unless that's something you require for other reasons.

How to query google-cloud-datastore to find records that dont have a specific property

I want to query cloud datastore and find all records that dont have a property 'foo'
I was looking at the docs, but didnt find anything there.
Any pointers for such query would be appreciated.
You cannot do that with a query. You will have to read all records and check if the property exists in each of those entities and do what's needed.
The documentation states that -
Entities lacking a property named in the query are ignored
Entities of the same kind need not have the same properties. To be
eligible as a query result, an entity must possess a value (possibly
null) for every property named in the query's filters and sort orders.
If not, the entity is omitted from the indexes used to execute the
query and consequently will not be included in the query's results.
Note: It is not possible to query for entities that are specifically
lacking a given property. One alternative is to add the property with
a null value, then filter for entities with null as the value of that
property.

Getting timestamp from Objecify entity?

Is it possible to obtain the timestamp that is used for optimistic concurrency control from an Objectify entity (or a lower-level part of the Google Datastore infrastructure) and if so, how?
It should be possible to obtain the version timestamp by specifying a field like this in your entity POJO:
#IgnoreSave long __version__;
The version # is found in the Entity properties with that key. If you're looking for official documentation, check javadocs (and source code) for Entity.VERSION_RESERVED_PROPERTY and Entities.getVersionProperty(Entity)
Why This Works
When you load a low-level Entity, it comes pre-populated with a synthetic property named __version__. Simply by adding a field to your Objectify POJO with that name, Objectify will load it out of the Entity. Use #IgnoreSave so that the value is only loaded, never saved.

Is there a way to use the DBC views to find the last date and time that a database schema was altered?

I would like to find the date and time that any schema modification has taken place on a particular database. Modifications are things like tables or columns that have been created, altered, or dropped. It does not include any data that has been inserted, updated, or deleted.
The reason why I need this is because I am writing a .NET utility that depends heavily on the data returned from dbc.tables, dbc.columns, and dbc.indices. Since querying these views can be a very expensive operation, I want to read it all into custom business objects and then serialize the objects to an XML file stored on disk. This way, I can just deserialize the data when I need it unless the database's current_timestamp is greater than or equal to the datetime of the last schema change, at which point I'll refresh the local XML file with the updated schema.
LastAlterTimestamp - If it is equal to CreateTimestamp then object has not been modified since being created or replaced. It is updated when an attribute specific to that data dictionary object was updated.
For example, DBC.Databases.LastAlterTimestamp is not update when a child object (table, view, macro, stored procedure, function, etc.) is added, removed, or altered. It is updated in situations such as when the password, default role, profile, or account is changed.

Resources