App42 Storage Service persists all data types supported by JSON. However, if your app data contains date field to be saved, it should be saved in UTC formatted String. If you save it in a normal format then you will not be able to apply date comparison query while searching documents. You can create UTC formatted String from Date object using our helper method and can save it thereafter.
You can easily convert date objects to UTC date strings using our method available in Util class as getUTCFormattedTimestamp(new Date())
in your app available in our respective SDKs. Here is the snippet for saving this in Android/JS and similarly can be done in rest of the SDKs too.
Android:
Date dateObj = new Date();
String dateString= Util.getUTCFormattedTimestamp(dateObj);
JSONObject jsonData = new JSONObject();
jsonData.put("name", "shashank");
jsonData.put("DOB", dateString);
Storage storageObj = storage.insertJSONDocument("dbName", "collectionName", jsonData);
System.out.println(storageObj);
JS:
In javascript you should use the format emitted by Date‘s toJSON method.
var storage = new App42Storage();
var dbName = "dbName";
var collectionName = "collectionName";
var jsonData= {};
jsonData.name = "shashank"
jsonData.DOB = new Date().toJSON().toString()
var result ;
storage.insertJSONDocument(dbName, collectionName, jsonData,{
success: function(object) {
// do code with your save object
},
error: function(error) {
// Handel error here
}
});
Once your JSON data is stored in App42 cloud, you can query your data with date objects using our query builder.
Android:
Date dateObj = new Date();
String dateString= Util.getUTCFormattedTimestamp(dateObj);
Query query = QueryBuilder.build("DOB", dateString, Operator.EQUALS);
Storage result = storage.findDocumentsByQuery("dbName", "collectionName", query);
System.out.println(result);
JS:
var dbName = "dbName";
var collectionName = "collectionName";
var queryBuilder = new QueryBuilder();
var query = queryBuilder.build("DOB", new Date().toJSON().toString(), Operator.EQUALS);
storage.findDocumentsByQuery(dbName, collectionName, query,{
success: function(object) {
console.log(object)
},
error: function(error) {
}
});
For more details see our JSON Data Storage Tutorial.
If you have any questions or need any further assistance to integrate this in your App, please feel free to write us at support@shephertz.com
Leave A Reply