title: Android Firebase orderByChild query
body: <p>I am currently writing an Android application using Firebase. I have a product model which has a city attribute.
  I am running the following query:</p>
  
  <pre><code>mDatabaseReference = FirebaseDatabase.getInstance().getReference();
  mDatabaseReference.keepSynced(true);
  
  productQuery = mDatabaseReference.child("products")
                     .orderByChild("city").equalTo(mCurrentUser).limitToFirst(100);
  </code></pre>
  
  <p>This query retrieves all products in the database regardless of the city.</p>
  
  <p>I have spent the last four hours reviewing similar questions on SO and trying different combinations of the query functions + indexing on rules to no avail.
  I have what I call a dumb solution which is re-duplicate the data under a city-products tree. However I am guessing that there is a better way leveraging orderByChild and I would appreciate if I could get some help to query the data by city.</p>
  
  <p>Thanks for your input </p>
  
  <p>Here's the CityFeed fragment - note: I checked mCurrentUserCity is set</p>
  
  <pre><code>public class CityFeedFragment extends BaseFragment {
      private RecyclerView mRecyclerView;
      private String mCurrentUserCity;
      private FirebaseProductAdapter mAdapter;
      private DatabaseReference mDatabaseReference;
      private LinearLayoutManager mManager;
      private Query productQuery;
      private SwipeRefreshLayout mSwipeRefreshLayout;
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_feed, container, false);
          mDatabaseReference = FirebaseDatabase.getInstance().getReference();
          mDatabaseReference.keepSynced(true);
  
          retrieveCity();
          fetchProducts();
  
          mAdapter = new FirebaseProductAdapter(Product.class,
                  R.layout.feed_item, ProductViewHolder.class,
                  productQuery.getRef(), getContext(), false);
  
          return view;
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
          mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
          mRecyclerView.setAdapter(mAdapter);
  
          mManager = new LinearLayoutManager(getActivity());
          mManager.setReverseLayout(true);
          mManager.setStackFromEnd(true);
  
          mRecyclerView.setHasFixedSize(true);
  
          mRecyclerView.setItemAnimator(new DefaultItemAnimator());
          mRecyclerView.setLayoutManager(mManager);
  
  
          mSwipeRefreshLayout.setOnRefreshListener(() -&gt; {
              fetchProducts();
              mAdapter.notifyDataSetChanged();
              mSwipeRefreshLayout.setRefreshing(false);
          });
  
      }
  
  
      private void fetchProducts() {
          productQuery = mDatabaseReference.child("products").orderByChild("city").equalTo(mCurrentUserCity);
      }
  
      private void retrieveCity() {
          mDatabaseReference.child("users").child(getUid()).addListenerForSingleValueEvent(
                  new ValueEventListener() {
                      @Override
                      public void onDataChange(DataSnapshot dataSnapshot) {
                          User user = dataSnapshot.getValue(User.class);
                          if (user == null) {
                              showToast(getResources().getString(R.string.empty_user));
                          } else {
                              mCurrentUserCity = user.getCity();
                          }
                      }
                      @Override
                      public void onCancelled(DatabaseError databaseError) {
                          showToast(databaseError.toString());
                          Log.w("Cityfeed", "getUser:onCancelled", databaseError.toException());
                      }
                  }
          );
      }
  
  }
  </code></pre>
  
  <p>Data structure
  <a href="https://i.stack.imgur.com/mAJN5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mAJN5.png" alt="enter image description here"></a></p>