Starting with a new collection depends on how the first entry was created (Geo)?

0 votes

Hello,

After so many hours, I finally found whats going on. Probably a bug on your end  ( or a fix needed at my end). The issue I run into is that I get no results back after I delete a collection and readd it. Lets assume that You have an empty db withno collections.  Here is the code I use to add the location ( I am not using the Geotag code as I need to update the same location over and over again as per on of the asked questions by fellow users)

 

dbName="mydb";
collectionName = "coll";
JSONObject jsonDoc = new JSONObject();
 double lat = loc.getLatitude(); //loc is my location obtained from gps
double lng = loc.getLongitude();
String key = "UserId";   
String value = "Random111";  
  try {
        jsonDoc.put(JSONUserId, Services.userName);
         jsonDoc.put(JSONDob, utcDateFormat);
        jsonDoc.put(JSONLoc, "["+lat+"," + lng+"]");
} catch (JSONException e) {
 
e.printStackTrace();
return ;
}
 
storageService.saveOrUpdateDocumentByKeyValue(dbName, collectionName, key, value, jsonDoc, null);  
 
 
 
Here is the code for getting the locations back
 
         double lat = loc.getLatitude(); //another loc that is very close (within meters)
         double lng = loc.getLongitude();
        BigDecimal distanceInKM = new BigDecimal(10);  //10km   
        GeoTag gp = new GeoTag();  
        gp.setLat(new BigDecimal(lat));  
        gp.setLng(new BigDecimal(lng));  
        GeoQuery query = QueryBuilder.buildGeoQuery(gp, GeoOperator.NEAR, distanceInKM);  
        // find documents by geo query.  
        
        storageService.findDocumentsByLocation(dbName, collectionName, query, new App42CallBack() { 
         ......
       });
 
 
 
If you do the above on a DB with no collection then the first call creates collecion COLL and add the entry above. I check on the dash board and the location column has the right location. However, when I query , I get the error msg "No collections exist"
 
 
Now, delete the collection from the dashboard. Create it again by adding the entry using the dashboard manually (using the same Geo coordinates) and run the code above then the locations are fetched. Even if the code above creates new locations, they are all fetched correctly. It seems that somehow, The first entry used to create the location will have to be either added manually from the dashboard OR use the code that does storageService.setGeoTag for the first location only. 
 
Are you aware of this issue? Any pointers?
 
Thank you so much
asked Dec 9, 2014 in App42 Cloud API-BaaS by AndroidFolk (107 points)

1 Answer

0 votes
 
Best answer

Hello AndroidFolk,

Geo query will find the json document in App42 data base which are saved insertJSONDocumentWithGeoTag functionality. Kindly insert your JSON document using this method and find the JSON document using findDocumentByLocation method.

About your second query to update the geo data, kindly find the below code snippet & let me know if it helps:

String dbName = "<Your_DataBase_Name>";

String collectionName = "<Your_Collection_Name>"; 

String docId = "docId";

JSONObject keys = new JSONObject();

keys.put("loc","[49.33,24.67]");

storageService.addOrUpdateKeys(dbName, collectionName, docId, keys,new App42CallBack() {

public void onSuccess(Object response) 

{

            Storage  storage  = (Storage )response;

            System.out.println("dbName is " + storage.getDbName());

            System.out.println("collection Name is " + storage.getCollectionName());

            ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList();          

            for(int i=0;i<jsonDocList.size();i++)

            {

                        System.out.println("objectId is " + jsonDocList.get(i).getDocId());  

                        System.out.println("CreatedAt is " + jsonDocList.get(i).getCreatedAt());  

                        System.out.println("UpdatedAtis " + jsonDocList.get(i).getUpdatedAt());  

                        System.out.println("Jsondoc is " + jsonDocList.get(i).getJsonDoc());  

            }  

}

public void onException(Exception ex) 

{

            System.out.println("Exception Message"+ex.getMessage());

}

});

We will soon add method to update geo data and can be directly used.

Let me know if it helps.

Himanshu Sharma

answered Dec 9, 2014 by hs00105 (2,005 points)
selected Dec 12, 2014 by AndroidFolk
Hello Thank you.
But I am able to find the geo that were inserted using the save/update method . even in the dashboard it shows the geo info correctly. I find it that it depends on the method of inserting the first entry into the collection. After that it does not matter how you insert or update entries
Hello AndroidFolk,
 
Yes, you are right whenever you insert location in new collection, we made it to as Geo spatial collection in our storage service. The difference between  Geo spatial collection & non Geo spatial collection is basically, you can't query Geo spatial query in non Geo spatial collection. In non Geo spatial collection, you can query compound statement to find the data however  Geo spatial query does not support compound query.

 
Let me know if it answer your question.
Himanshu Sharma
Hello Himanshu,
I am kinda got the point but to be honest, I am still a bit confused.
1- So if I start new collection with insertJSONDocumentWithGeoTag OR using the dashboard "Save JSON" Does that make it Geo Spatial collection or not?  

2- Also, if I do that, can I then safely insert/update Geo data using saveOrUpdateDocumentByKeyValue  and query the databack using findDocumentsByLocation? ( I think this is behaviour I noticed)

Basically what I am trying to do is to be able to insert new entry or update an entry using JsonDoc info ie key/value pair as doing it by jsonDocId is a bit  unreasonable in my case ( seems like another ID I need to track per user). Then I would like to get the the geo information based on some Geo info and distance.
Thank you again and sorry for the confusion
Hello AndroidFolk,

To answer your first query, both are same if you save geo data from dashboard or using insertDocumentWithGeoTag.

To answer your second query, yes you can update the data using saveOrUpdateDocumentByKeyValue method & fetch it back easily by using findDocumentByLocation method.

Let me know if it answer your question.
Himanshu Sharma
Thank you. Then that means whenever I create a new collection then I need to add one dummy location from the dashboard to make it geo-spatial before I add further on from the code using saveOrUpdateDocumentByKeyValue
I think this needs to be documented somewhere as definitely not intuitive
Thank you
In your scenario, yes you have to do it. Let us know if you have more question for us. Also requested you to close this thread, if your query has been resolved.
Thanks,
Himanshu Sharma
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...