Skip to content

Instantly share code, notes, and snippets.

View trishagee's full-sized avatar

Trisha Gee trishagee

View GitHub Profile
@Unroll
def 'should generate index name #indexName for #index'() {
expect:
index.getName() == indexName;
where:
index | indexName
new Index('x') | 'x_1'
new Index('x', OrderBy.ASC) | 'x_1'
new Index('x', OrderBy.DESC) | 'x_-1'
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("TheDatabaseName");
DBCollection collection = database.getCollection("TheCollectionName");
List<Integer> books = Arrays.asList(27464, 747854);
DBObject person = new BasicDBObject("_id", "jo")
.append("name", "Jo Bloggs")
.append("address", new BasicDBObject("street", "123 Fake St")
.append("city", "Faketon")
.append("state", "MA")
.append("zip", 12345))
.append("books", books);
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("Examples");
DBCollection collection = database.getCollection("people");
collection.insert(person);
public static final DBObject toDBObject(Person person) {
return new BasicDBObject("_id", person.getId())
.append("name", person.getName())
.append("address", new BasicDBObject("street", person.getAddress().getStreet())
.append("city", person.getAddress().getTown())
.append("phone", person.getAddress().getPhone()))
.append("books", person.getBookIds());
}
collection.insert(PersonAdaptor.toDBObject(myPerson));
DBObject query = new BasicDBObject("_id", "jo");
DBCursor cursor = collection.find(query);