简体   繁体   中英

Retrieve multiple entries from 2 collections

Scenario:

I have two JSON's stored in mongodb of the following format: (They are from Yelp Educational)

JSON1: (Yelp Business)

   {
     "business_id":"tl9XIP5trlkcuSfTQqe5jg",
     "full_address":"632 N Estrella Pkwy\nGoodyear, AZ 85338",
     "hours":{
   },
   "open":true,
   "categories":[
   "Fast Food",
   "Restaurants"
   ],
   "city":"Goodyear",
   "review_count":6,
   "name":"McDonalds",
   "neighborhoods":[

   ],
   "longitude":-112.39319500000001,
   "state":"AZ",
   "stars":2.0,
   "latitude":33.453887000000002,
   "attributes":{
   "Take-out":true,
   "Wi-Fi":"free",
   "Drive-Thru":true,
   "Alcohol":"none",
   "Caters":false,
   "Noise Level":"average",
   "Takes Reservations":false,
   "Delivery":false,
   "Parking":{
     "garage":false,
     "street":false,
     "validated":false,
     "lot":false,
     "valet":false
  },
  "Has TV":true,
  "Outdoor Seating":false,
  "Attire":"casual",
  "Waiter Service":false,
  "Accepts Credit Cards":true,
  "Good for Kids":true,
  "Price Range":1
   },
   "type":"business"
}

JSON2: (Reviews)

{
   "votes":{
   "funny":0,
   "useful":0,
   "cool":0
   },
   "user_id":"pNvoNTu6U7Ek2w_xe4QO-w",
   "review_id":"qyUlYgt68wexC_6qLL0sKg",
   "stars":1,
   "date":"2012-03-12",
   "text":"The worst McDonalds I've ever been to. The burgers are barely room temp and the cheese is barely melted on them even though they microwave them! Which is disgusting as it is. Chicken nuggets are always old and greasy (and again never hot enough). Filet o fish cold and gross..and either unmelted cheese or cheese that was nuked so long that it is like plastic. Nasty. I've never had a decent meal here. Haven't gone in months. Gross.",
   "type":"review",
   "business_id":"tl9XIP5trlkcuSfTQqe5jg"
}

Both of them have same business_id

Problem Statement: How can I wrote a query so that I can fetch "category":"Fast food" and at the same time get the reviews?

I am able to retrieve one but not reviews. Please comment!

Code:

System.out.println("Fast Food Restaurants");

BasicDBObject rest = new BasicDBObject();
rest.put("categories", "Indian");
DBCursor cursor2 = table.find(rest);
while(cursor2.hasNext()){ //display all fast food restaurants
    System.out.println(cursor2.next());
}

How can I display the ratings from the other JSON?

Thanks for your time!

You have to do in two steps. In Mongo there is no join. So there is no way you can write a single query to access the data from two different collection.

you fetched the business in the following step -

BasicDBObject rest = new BasicDBObject();
rest.put("categories", "Indian");

Next step -

DBObject query = new BasicDBObject();
DBCursor cursor3 = null;
DBObject dbObject = null;
while(cursor2.hasNext()){ //display all fast food restaurants
    dbObject = cursor2.next();
    query.put("business_id",dbObject.get("business_id"))//get the business_id from dbObject returned from above
    cursor3 = table2.find(query); // here you have all the reviews for that business.
    //next loop through cursor3 for your reviews
}

In your mongodb documents, if you are storing everything as String, then you can do dbObject.toString() and get the json and then convert to java pojo using jackson of gson.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM