For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.
Querying data
After you create an index, we can query the data in your database.
For more details, see CDTDatastore Query documentation.
For more details, see Cloudant® Sync Query documentation.
For query operations on a remote store, see the Cloudant Query API.
Procedure
- Create and run a query on iOS.
BEFORE (with IMFData/CloudantToolkit):
// Use an existing store CDTStore *store = existingStore; NSPredicate *queryPredicate = [NSPredicate predicateWithFormat:@"(year = 2006)"]; CDTCloudantQuery *query = [[CDTCloudantQuery alloc] initDataType:[store.mapper dataTypeForClassName:NSStringFromClass([Automobile class])] withPredicate:queryPredicate]; [store performQuery:query completionHandler:^(NSArray *results, NSError *error) { if(error){ // Handle error }else{ // Use result of query. Result will be Automobile objects. } }];
// Use an existing store let store:CDTStore = existingStore let queryPredicate:NSPredicate = NSPredicate(format:"(year = 2006)") let query:CDTCloudantQuery = CDTCloudantQuery(dataType: "Automobile", withPredicate: queryPredicate) store.performQuery(query, completionHandler: { (results:[AnyObject]!, error:NSError!) -> Void in if nil != error { // Handle error } else { // Use result of query. Result will be Automobile objects. } })
AFTER (with Cloudant Sync):
// Use an existing store CDTDatastore *datastore = existingDatastore; CDTQResultSet *results = [datastore find:@{@"@datatype" : @"Automobile", @"year" : @2006}]; if(results){ // Use results }
// Use an existing store let datastore:CDTDatastore = existingDatastore let results:CDTQResultSet? = datastore.find(["@datatype" : "Automobile", "year" : 2006]) if(results == nil){ // Handle error }
Create and run a query for objects on Android.
To run a query for objects, create a Cloudant query with the query filters on data type. Run the query against a Store object.
BEFORE (with IMFData/CloudantToolkit):
// Use an existing store Store store = existingStore; // Create data type predicate Map<String, Object> dataTypeEqualityOpMap = new HashMap<String, Object>(); dataTypeEqualityOpMap.put("$eq", "Automobile"); Map<String, Object> dataTypeSelectorMap = new HashMap<String, Object>(); dataTypeSelectorMap.put("@datatype", dataTypeEqualityOpMap); // Create year predicate Map<String, Object> yearEqualityOpMap = new HashMap<String, Object>(); yearEqualityOpMap.put("$eq", 2006); Map<String, Object> yearSelectorMap = new HashMap<String, Object>(); yearSelectorMap.put("year", yearEqualityOpMap); // Add predicates to AND compound predicate List<Map<String, Object>> andPredicates = new ArrayList<Map<String, Object>>(); andPredicates.add(dataTypeSelectorMap); andPredicates.add(yearSelectorMap); Map<String, Object> andOpMap = new HashMap<String, Object>(); andOpMap.put("$and", andPredicates); Map<String, Object> cloudantQueryMap = new HashMap<String, Object>(); cloudantQueryMap.put("selector", andOpMap); // Create a Cloudant Query Object CloudantQuery query = new CloudantQuery(cloudantQueryMap); // Run the Cloudant Query against a Store Task<List> queryTask = store.performQuery(query); queryTask.continueWith(new Continuation<List, Object>() { @Override public Object then(Task<List> task) throws Exception { if(task.isFaulted()){ // Handle Error }else{ List queryResult = task.getResult(); // Use queryResult to do something } return null; } });
AFTER (with Cloudant Sync):
// Use an existing store Datastore datastore = existingStore; IndexManager indexManager = existingIndexManager; // Create data type predicate Map<String, Object> dataTypeEqualityOpMap = new HashMap<String, Object>(); dataTypeEqualityOpMap.put("$eq", "Automobile"); Map<String, Object> dataTypeSelectorMap = new HashMap<String, Object>(); dataTypeSelectorMap.put("@datatype", dataTypeEqualityOpMap); // Create year predicate Map<String, Object> yearEqualityOpMap = new HashMap<String, Object>(); yearEqualityOpMap.put("$eq", 2006); Map<String, Object> yearSelectorMap = new HashMap<String, Object>(); yearSelectorMap.put("year", yearEqualityOpMap); // Add predicates to AND compound predicate List<Map<String, Object>> andPredicates = new ArrayList<Map<String, Object>>(); andPredicates.add(dataTypeSelectorMap); andPredicates.add(yearSelectorMap); Map<String, Object> selectorMap = new HashMap<String, Object>(); selectorMap.put("$and", andPredicates); // Run the query against a Store QueryResult result = indexManager.find(selectorMap);
Parent topic: Migrating apps storing mobile data in Cloudant with IMFData or Cloudant SDK