在Java中查找用户的MongoDb
我的数据库中有这三个用户,他们使用以下键:
乔恩-111
乔恩222
乔恩333
我目前编写的代码只能通过密钥在数据库中找到一个用户。 如何返回所有这些用户
public void getMongoDB() {
DB db = mongo.getDB("people");
DBCollection table = db.getCollection("users");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("key", "Jon-111"); // what do I do here to return all the 'Jon' users?
DBCursor cursor = table.find(searchQuery);
int dbSize = cursor.size(); // size is 1
while (cursor.hasNext()) {
logger.debug(""+cursor.next()+"\n");
}
logger.debug("users table");
}
提前谢谢
# 1 楼答案
可以使用regex模式传入值。如下所示: