Created
September 15, 2011 14:11
-
-
Save bwaldvogel/1219330 to your computer and use it in GitHub Desktop.
simple or query with mongodb java driver
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 java.util.ArrayList; | |
import java.util.List; | |
import com.mongodb.BasicDBObject; | |
import com.mongodb.DBCollection; | |
import com.mongodb.DBObject; | |
import com.mongodb.Mongo; | |
public class SimpleOrQuery { | |
public static void main(String[] args) throws Exception { | |
Mongo mongo = new Mongo("localhost"); | |
try { | |
DBCollection collection = mongo.getDB("test").getCollection("foo"); | |
collection.insert(new BasicDBObject("foo", "bar1")); | |
collection.insert(new BasicDBObject("foo", "bar2")); | |
collection.insert(new BasicDBObject("foo", "bar3")); | |
List<BasicDBObject> coll = new ArrayList<BasicDBObject>(); | |
coll.add(new BasicDBObject("foo", "bar1")); | |
coll.add(new BasicDBObject("foo", "bar2")); | |
BasicDBObject query = new BasicDBObject("$or", coll); | |
// does only output two objects | |
for (DBObject o : collection.find(query)) { | |
System.out.println(o); | |
} | |
} finally { | |
mongo.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment