Created
December 5, 2014 11:50
-
-
Save antonarhipov/4a9472f8d43d486147a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.*; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.xml.ws.Endpoint; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.StringJoiner; | |
public class Get extends javax.servlet.http.HttpServlet { | |
@Override | |
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
resp.setHeader("Content-Type", "application/json"); | |
PrintWriter writer = resp.getWriter(); | |
MongoClient mongoClient = new MongoClient("localhost", 27017); | |
DB db = mongoClient.getDB("mydb"); | |
DBCollection supplements = db.getCollection("supplements"); | |
DBCollection supplementPrices = db.getCollection("supplement_prices"); | |
try (DBCursor cursor = supplements.find()) { | |
cursor.batchSize(2); | |
StringJoiner sj = new StringJoiner(",", "[", "]"); | |
while (cursor.hasNext()) { | |
DBObject dbObject = cursor.next(); | |
System.out.println(dbObject); | |
Object priceId = dbObject.get("price_id"); | |
BasicDBObject query = new BasicDBObject("id", new BasicDBObject("$eq", String.valueOf(priceId))); | |
DBObject priceObject = supplementPrices.findOne(query); | |
System.out.println(priceObject); | |
String x = dbObject.toString(); | |
// System.out.println(x + " = " + priceObject.toString()); | |
sj.add(x); | |
} | |
writer.write(sj.toString()); | |
} | |
writer.flush(); | |
writer.close(); | |
} | |
} | |
//data | |
> db.supplements.find() | |
{ "_id" : ObjectId("54576ae0ac8d18d02b146498"), "name" : "Arden Grange", "weight" : "1kg", "price_id" : "10" } | |
{ "_id" : ObjectId("54576ae3ac8d18d02b146499"), "name" : "Arden Grange", "weight" : "2kg", "price_id" : "20" } | |
{ "_id" : ObjectId("54576ae5ac8d18d02b14649a"), "name" : "Arden Grange", "weight" : "3kg", "price_id" : "30" } | |
{ "_id" : ObjectId("54576aeaac8d18d02b14649b"), "name" : "Arden Grange", "weight" : "5kg", "price_id" : "40" } | |
{ "_id" : ObjectId("54576b26ac8d18d02b14649c"), "name" : "BLUE", "weight" : "5kg", "price_id" : "20" } | |
{ "_id" : ObjectId("54576b29ac8d18d02b14649d"), "name" : "BLUE", "weight" : "10kg", "price_id" : "50" } | |
{ "_id" : ObjectId("54576b3fac8d18d02b14649e"), "name" : "Chappie", "weight" : "100g", "price_id" : "10" } | |
{ "_id" : ObjectId("54576b42ac8d18d02b14649f"), "name" : "Chappie", "weight" : "300g", "price_id" : "20" } | |
{ "_id" : ObjectId("54576b46ac8d18d02b1464a0"), "name" : "Chappie", "weight" : "1kg", "price_id" : "30" } | |
{ "_id" : ObjectId("54576b49ac8d18d02b1464a1"), "name" : "Chappie", "weight" : "3kg", "price_id" : "40" } | |
{ "_id" : ObjectId("54576b64ac8d18d02b1464a2"), "name" : "Royal Canin", "weight" : "1kg", "price_id" : "20" } | |
{ "_id" : ObjectId("54576b67ac8d18d02b1464a3"), "name" : "Royal Canin", "weight" : "3kg", "price_id" : "30" } | |
{ "_id" : ObjectId("54576b69ac8d18d02b1464a4"), "name" : "Royal Canin", "weight" : "4kg", "price_id" : "40" } | |
{ "_id" : ObjectId("54576b6eac8d18d02b1464a5"), "name" : "Royal Canin", "weight" : "10kg", "price_id" : "50" } | |
> db.supplement_prices.find() | |
{ "_id" : ObjectId("54576f63ac8d18d02b1464a6"), "id" : "10", "price" : "10EUR" } | |
{ "_id" : ObjectId("54576f6aac8d18d02b1464a7"), "id" : "20", "price" : "20EUR" } | |
{ "_id" : ObjectId("54576f6fac8d18d02b1464a8"), "id" : "30", "price" : "30EUR" } | |
{ "_id" : ObjectId("54576f75ac8d18d02b1464a9"), "id" : "40", "price" : "40EUR" } | |
{ "_id" : ObjectId("54576f7fac8d18d02b1464aa"), "id" : "50", "price" : "50EUR" } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment