Skip to content

Instantly share code, notes, and snippets.

@trnl
Created April 8, 2012 10:52
Show Gist options
  • Save trnl/2336587 to your computer and use it in GitHub Desktop.
Save trnl/2336587 to your computer and use it in GitHub Desktop.
import com.mongodb.*;
import org.bson.BSON;
import java.util.regex.Pattern;
/**
* @author trnl
* Date: 12/27/11
* Time: 3:49 PM
*/
public class Tester {
public static void main(String[] args) throws Exception, InterruptedException {
DBCollection c = new Mongo().getDB("tmp").getCollection("tmp");
c.drop();
c.insert(o("a","aaa"));
c.insert(o("a", "aab"));
c.insert(o("a", "abb"));
System.out.println(c.find(o("a", Pattern.compile("aa.*"))).toArray());
System.out.println(c.find(o("a", o("$regex","aa.*"))).toArray());
System.out.println(BSON.encode(o("a", Pattern.compile("aa.*"))).length);
System.out.println(BSON.encode(o("a", o("$regex","aa.*"))).length);
}
public static DBObject o(Object... args) {
DBObject o = new BasicDBObject();
for (int i = 0; i < args.length; i += 2) {
o.put((String) args[i], args[i + 1]);
}
return o;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment