Querying data from JSONStore collection

improve this page | report issue

Setting up the React Native development environment

Follow the instructions provided in the React Native Gettings Started Page to set up your machine for React Native development.

Adding the JSONStore SDK to your React Native app

The JSONStore SDK for React Native is available as a React Native module from npm.

Getting started with a new React Native project

  1. Create a new React Native project.
     react-native init MyReactApp
  2. Add the MobileFirst SDK to your app.
     cd MyReactApp
     npm install react-native-ibm-mobilefirst-jsonstore --save
  3. Link all native dependencies to your app.
    react-native link

Querying data from JSONStore collection

Rarely would you want to get all the documents in a collection at the same time. In general, you need the ability to query the existing data in your collection.

Inside your App.js you need to import the following packages:

import { JSONStoreCollection, WLJSONStore } from 'react-native-ibm-mobilefirst-jsonstore';

There are two steps for querying data from a JSONStore collection:

  1. Opening a Collection, opening a collection allows us to interact with it.
     WLJSONStore.openCollections(['favourites']).then(data => { console.log(data); }).catch(err =>{ console.log(err); });
  2. Fetching data from a Collection: After you have opened a collection, you can fetch the documents based on given query. For querying JSONStore two classes are provided to work with JSONStoreQuery and JSONStoreQueryPart.
    You can use multiple JSONStoreQueryPart objects for same call by passing each JSONStoreQueryPart object in an array. Multiple JSONStoreQueryPart objects are joined using an OR statement. Multiple conditions for one JSONStoreQueryPart are joined using an AND statement.

    Refer to the following code:

     var favCollection = new JSONStoreCollection('favourites');
     var queryPart1 = new JSONStoreQueryPart();
     queryPart1.addBetween("age", 21, 50);
     var queryPart2 = new JSONStoreQueryPart();
     queryPart2.addEqual("gender", "female");
     // Notice how multiple JSONStoreQueryPart objects are passed in an array to build a complex query
     // The following call will return - all the Documents that has either
     // "gender" set to "female" OR has "age" between range 21 - 50
     favCollection.findDocuments([queryPart1, queryPart2])
     .then(data => {
     	console.log("Succesfully fetched all documents from collection!"));
     	console.log("Data: " + JSON.stringify(data));
     .catch(err => {
     	console.log("Error while fetching data from collection. Reason : " + err);
Last modified on November 21, 2018