public static List<String> queryMongoForSimilar(String courtId, String facets) throws Exception { // Query mongo for other facets with these values. System.out.println("Court id is: " + courtId); initializeMap(); List<String> facetList = facetStringToListOfKeys(facets); // Facet list has the keys to get. Query mongo for the document with given courtId, then pull // the values of these keys. MongoConnection mc = new MongoConnection("supremeCourtDataDB.properties"); MongoCollection<Document> collection = mc.getCollection(); int courtIdInt = Integer.parseInt(courtId); Document d = collection.find(eq("courtId", courtIdInt)).first(); System.out.println("Doc with this court id: " + d); Document queryDoc = new Document(); // Build query based on given keys and their values in this doc. for (int i = 0; i < facetList.size(); i++) { String key = facetList.get(i); String tempString; int tempInt; try { tempString = d.getString(key); // queryJSON.put(key, tempString); queryDoc.put(key, tempString); } catch (ClassCastException c) { tempInt = d.getInteger(key); queryDoc.put(key, tempInt); // queryJSON.put(key, tempInt); } } List<String> resultList = new ArrayList<String>(); System.out.println("Querying mongo."); FindIterable<Document> result = collection.find(queryDoc); for (Document doc : result) { String temp = Integer.toString(doc.getInteger("courtId")); resultList.add(temp); } System.out.println("Done!"); return resultList; }
@Test public void canFindUsingSubProperty() throws Exception { /* given */ collection.save(new Friend("John", new Coordinate(2, 31))); /* when */ Iterator<Friend> results = collection.find("{'coordinate.lat':2}").as(Friend.class).iterator(); /* then */ assertThat(results.next().getCoordinate().lat).isEqualTo(2); assertThat(results.hasNext()).isFalse(); }
@Test public void canFind() throws Exception { /* given */ Friend friend = new Friend(new ObjectId(), "John"); collection.save(friend); /* when */ Iterator<Friend> friends = collection.find("{name:'John'}").as(Friend.class).iterator(); /* then */ assertThat(friends.hasNext()).isTrue(); assertThat(friends.next().getName()).isEqualTo("John"); assertThat(friends.hasNext()).isFalse(); }
@Test public void canFindWithOid() throws Exception { /* given */ ObjectId id = new ObjectId(); Friend john = new Friend(id, "John"); collection.save(john); Iterator<Friend> friends = collection.find("{_id:{$oid:#}}", id.toString()).as(Friend.class).iterator(); /* then */ assertThat(friends.hasNext()).isTrue(); assertThat(friends.next().getId()).isEqualTo(id); }
@Test public void shouldFailWhenUnableToUnmarshallResult() throws Exception { /* given */ collection.insert("{error: 'NotaDate'}"); /* when */ Iterator<ErrorObject> results = collection.find().as(ErrorObject.class).iterator(); try { results.next(); fail(); } catch (MarshallingException e) { assertThat(e.getMessage()).contains(" \"error\" : \"NotaDate\""); } }
@Test public void canFindWithEmptySelector() throws Exception { /* given */ collection.insert("{name:'John'}"); collection.insert("{name:'Smith'}"); collection.insert("{name:'Peter'}"); /* when */ Iterable<Friend> friends = collection.find().as(Friend.class); /* then */ assertThat(friends.iterator().hasNext()).isTrue(); for (Friend friend : friends) { assertThat(friend.getName()).isIn("John", "Smith", "Peter"); } }
@Test public void canFindWithStringAsObjectId() { /* given */ String id = ObjectId.get().toString(); ExposableFriend friend = new ExposableFriend(id, "John"); collection.save(friend); /* when */ Iterator<ExposableFriend> friends = collection.find(withOid(friend.getId())).as(ExposableFriend.class).iterator(); /* then */ ExposableFriend john = friends.next(); assertThat(john.getId()).isEqualTo(id); assertThat(john.getName()).isEqualTo("John"); assertThat(friends.hasNext()).isFalse(); }
public void run() { // Random random = new Random(); for (int i = 0; i < times; i++) { try { MongoCollection collection = mongo.getDB("goojia").getCollection("goojia_common.ppc_map"); BSONDocument doc = new MongoDocument(); doc.put("city", "sh"); doc.put("category", 1); doc.put("relate_id", 123); doc.put("relate_id_2", 0); collection.find(doc).toList(); } catch (Exception e) { e.printStackTrace(); } } countDownLatch.countDown(); System.out.println("a"); }