@Test public void shouldUseDefaultWriteConcern() throws Exception { Friend friend = new Friend("John", "22 Wall Street Avenue"); WriteResult writeResult = collection.save(friend); assertThat(writeResult.getLastConcern()) .isEqualTo(collection.getDBCollection().getWriteConcern()); }
@Test public void canSaveWithACustomTypeId() throws Exception { ExternalFriend john = new ExternalFriend(999, "Robert"); collection.save(john); ExternalFriend result = collection.findOne().as(ExternalFriend.class); assertThat(result.getId()).isEqualTo(999); }
@Test public void canSaveAnObjectWithoutIdAnnotation() throws Exception { Coordinate noId = new Coordinate(123, 1); collection.save(noId); Coordinate result = collection.findOne().as(Coordinate.class); assertThat(result).isNotNull(); assertThat(result.lat).isEqualTo(123); }
@Test public void canSaveWithAnEmptyObjectIdAsString() throws Exception { ExposableFriend john = new ExposableFriend("Robert"); collection.save(john); ExposableFriend result = collection.findOne().as(ExposableFriend.class); assertThat(result).isNotNull(); assertThat(result.getId()).isNotNull(); }
@Test public void canSaveWithObjectId() throws Exception { ObjectId oid = ObjectId.get(); Friend john = new Friend(oid, "John"); collection.save(john); Friend result = collection.findOne(oid).as(Friend.class); assertThat(result.getId()).isEqualTo(oid); assertThat(oid.isNew()).isFalse(); // insert }
@Test public void canSave() throws Exception { Friend friend = new Friend("John", "22 Wall Street Avenue"); collection.save(friend); Friend john = collection.findOne("{name:'John'}").as(Friend.class); assertThat(john).isNotNull(); assertThat(john.getId()).isNotNull(); assertThat(john.getId().isNew()).isFalse(); }
@Test public void canSaveWithObjectIdAsString() throws Exception { String id = ObjectId.get().toString(); ExposableFriend john = new ExposableFriend(id, "Robert"); collection.save(john); ExposableFriend result = collection.findOne().as(ExposableFriend.class); assertThat(result).isNotNull(); assertThat(result.getId()).isEqualTo(id); }
@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(); }
@Override public BasicDBObject next() { DBObject next = iterator.next(); if (log.isDebugEnabled()) { log.debug( "<-" + mongoCollection.getDbCollection().getName() + "-- " + mongoCollection.toString(next)); } return (BasicDBObject) next; }
@Test public void canUpdateWithACustomTypeId() throws Exception { ExternalFriend friend = new ExternalFriend(999, "Robert"); collection.save(friend); friend.setName("Robert"); collection.save(friend); ExternalFriend result = collection.findOne().as(ExternalFriend.class); assertThat(result.getId()).isEqualTo(999); }
@Test public void canUpdateAnEntity() throws Exception { Friend john = new Friend("John", "21 Jump Street"); collection.save(john); john.setAddress("new address"); collection.save(john); ObjectId johnId = john.getId(); Friend johnWithNewAddress = collection.findOne(johnId).as(Friend.class); assertThat(johnWithNewAddress.getId()).isEqualTo(johnId); assertThat(johnWithNewAddress.getAddress()).isEqualTo("new address"); }
@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 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 canUpdateWithObjectIdAsString() throws Exception { String id = ObjectId.get().toString(); ExposableFriend robert = new ExposableFriend(id, "Robert"); collection.save(robert); String johnId = robert.getId(); robert.setName("Hue"); // <-- "famous" french Robert collection.save(robert); ExposableFriend robertHue = collection.findOne("{_id:#}", johnId).as(ExposableFriend.class); assertThat(robertHue.getId()).isEqualTo(johnId); assertThat(robertHue.getName()).isEqualTo("Hue"); }
@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 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; }
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"); }
@Test public void canSaveWithWriteConcern() throws Exception { Friend friend = new Friend("John", "22 Wall Street Avenue"); WriteResult writeResult = collection.withWriteConcern(WriteConcern.SAFE).save(friend); assertThat(writeResult.getLastConcern()).isEqualTo(WriteConcern.SAFE); }
@Test public void shouldFailWhenMarshallerFail() throws Exception { try { collection.save(new ErrorObject()); Assert.fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).contains("Unable to save object"); } }
public static void main(String[] args) throws Exception { Configuration config = new Configuration(); Mongo mongo = new MongoImpl(config, "127.0.0.1:27018", "127.0.0.1:27017", "127.0.0.1:27019"); 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.save(doc); Thread.sleep(20000); doc = new MongoDocument(); doc.put("city", "bj"); doc.put("category", 12); doc.put("relate_id", 12344); doc.put("relate_id_2", 0); collection.save(doc); mongo.close(); }
@Test public void canFindWithReadPreference() throws Exception { /* given */ Friend friend = new Friend(new ObjectId(), "John"); collection.save(friend); /* when */ Iterator<Friend> friends = collection .withReadPreference(ReadPreference.primaryPreferred()) .find("{name:'John'}") .as(Friend.class) .iterator(); /* then */ assertThat(friends.hasNext()).isTrue(); assertThat(friends.next().getName()).isEqualTo("John"); assertThat(friends.hasNext()).isFalse(); // warning: we cannot check that ReadPreference is really used by driver, this unit test only // checks the API }
@Override public int compareTo(MongoCollection otherCollection) { return this.name.compareTo(otherCollection.getName()); }
@Test(expected = UnsupportedOperationException.class) public void containsAll() { collection = new MongoCollection<String>(null, null); collection.containsAll(null); }
@Test(expected = IllegalArgumentException.class) public void shouldRejectNullObjectId() throws Exception { collection.findOne((ObjectId) null); }