mongodb- Java API 查询操作
package com.x.mongodb;import java.net.UnknownHostException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.bson.types.BasicBSONList;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* 查询
*: $in{ "name1" : { "$in" : [ "zhou28-1" , "zhou29"]}}
*: $nin{ "age" : { "$nin" : [ 1 , 2]}}
*: $or{ "$or" : [ { "name1" : { "$in" : [ "zhou28-1" , "zhou29"]}}]}
*: $or and{ "$or" : [ { "name1" : { "$in" : [ "zhou28-1" , "zhou29"]}}] , "name2" : "zhou"}
*: 范围查询 { "age" : { "$gte" : 2 , "$lte" : 21}}
*: $ne{ "age" : { "$ne" : 23}}
*: $lt { "age" : { "$lt" : 23}}
* @author java2000_wl
* @version 1.0
*/
public final class MongoDBUtil {
private static final String HOST = "127.0.0.1";
private static final String dbName = "test";
private static Mongo mongo;
private static DB db;
static {
try {
mongo = new Mongo(HOST);
db = mongo.getDB(dbName);
// db.authenticate(username, passwd)
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
private MongoDBUtil() {
}
/**
* 判断集合是否存在
* ------------------------------
* @param collectionName
* @return
*/
public static boolean collectionExists(String collectionName) {
return db.collectionExists(collectionName);
}
/**
* 查询单个,按主键查询
* ------------------------------
* @param id
* @param collectionName
*/
public static void findById(String id, String collectionName) {
Map map = new HashMap();
map.put("_id", new ObjectId(id));
findOne(map, collectionName);
}
/**
* 查询单个
* ------------------------------
* @param map
* @param collectionName
*/
public static void findOne(Map map, String collectionName) {
DBObject dbObject = getMapped(map);
DBObject object = getCollection(collectionName).findOne(dbObject);
print(object);
}
/**
* 查询全部
* ------------------------------
* @param cursor
* @param collectionName
*/
public static void findAll(CursorObject cursor, String collectionName) {
find(new HashMap(), cursor,collectionName);
}
/**
* count
* ------------------------------
* @param map
* @param collectionName
* @return
*/
public static long count(Map map, String collectionName) {
DBObject dbObject = getMapped(map);
return getCollection(collectionName).count(dbObject);
}
/**
* 按条件查询
* 支持skip,limit,sort
* ------------------------------
* @param map
* @param cursor
* @param collectionName
*/
public static void find(Map map, CursorObject cursor, String collectionName) {
DBObject dbObject = getMapped(map);
find(dbObject, cursor, collectionName);
}
/**
* 查询
* ------------------------------
* @param dbObject
* @param cursor
* @param collectionName
*/
public static void find(DBObject dbObject, final CursorObject cursor,String collectionName) {
CursorPreparer cursorPreparer= cursor == null ? null : new CursorPreparer() {
public DBCursor prepare(DBCursor dbCursor) {
if (cursor == null) {
return dbCursor;
}
if (cursor.getLimit()0) {
cursorToUse = cursorToUse.limit(cursor.getLimit());
}
if (cursor.getSortObject() != null) {
cursorToUse = cursorToUse.sort(cursor.getSortObject());
}
return cursorToUse;
}
};
find(dbObject, cursor, cursorPreparer, collectionName);
}
/**
* 查询
* ------------------------------
* @param dbObject
* @param cursor
* @param cursorPreparer
* @param collectionName
*/
public static void find(DBObject dbObject, CursorObject cursor, CursorPreparer cursorPreparer, String collectionName) {
DBCursor dbCursor = getCollection(collectionName).find(dbObject);
if (cursorPreparer != null) {
dbCursor = cursorPreparer.prepare(dbCursor);
}
Iterator iterator = dbCursor.iterator();
while (iterator.hasNext()) {
print(iterator.next());
}
}
/**
* 获取集合(表)
* ------------------------------
* @param collectionName
* @return
*/
public static DBCollection getCollection(String collectionName) {
return db.getCollection(collectionName);
}
/**
* 获取所有集合名称
* ------------------------------
* @return
*/
public static Set getCollection() {
return db.getCollectionNames();
}
/**
* 创建集合
* ------------------------------
* @param collectionName
* @param options
*/
public static void createCollection(String collectionName, DBObject options) {
db.createCollection(collectionName, options);
}
/**
* 删除
* ------------------------------
* @param collectionName
*/
public static void dropCollection(String collectionName) {
DBCollection collection = getCollection(collectionName);
collection.drop();
}
/**
*
* ------------------------------
* @param map
* @return
*/
private static DBObject getMapped(Map map) {
DBObject dbObject = new BasicDBObject();
Iterator iterable = map.entrySet().iterator();
while (iterable.hasNext()) {
Entry entry = iterable.next();
Object value = entry.getValue();
String key = entry.getKey();
if (key.startsWith("$") && value instanceof Map) {
BasicBSONList basicBSONList = new BasicBSONList();
Map conditionsMap = ((Map)value);
Set keys = conditionsMap.keySet();
for (String k : keys) {
Object conditionsValue = conditionsMap.get(k);
if (conditionsValue instanceof Collection) {
conditionsValue =convertArray(conditionsValue);
}
DBObject dbObject2 = new BasicDBObject(k, conditionsValue);
basicBSONList.add(dbObject2);
}
value= basicBSONList;
} else if (value instanceof Collection) {
value =convertArray(value);
} else if (!key.startsWith("$") && value instanceof Map) {
value = getMapped(((Map)value));
}
dbObject.put(key, value);
}
return dbObject;
}
private static Object[] convertArray(Object value) {
Object[] values = ((Collection)value).toArray();
return values;
}
private static void print(DBObject object) {
Set keySet = object.keySet();
for (String key : keySet) {
print(object.get(key));
}
}
private static void print(Object object) {
System.out.println(object.toString());
}
}package com.x.mongodb;
/**
* 排序规则
* @author java2000_wl
* @version 1.0
*/
public enum Order {
ASC, DESC
}package com.x.mongodb;
import java.util.LinkedHashMap;
import java.util.Map;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
* 排序对象
* @author java2000_wl
* @version 1.0
*/
public class Sort {
/** key为排序的名称, value为顺序 */
private Map field = new LinkedHashMap();
public Sort() {
}
public Sort(String key, Order order) {
field.put(key, order);
}
public Sort on(String key, Order order) {
field.put(key, order);
return this;
}
public DBObject getSortObject() {
DBObject dbo = new BasicDBObject();
for (String k : field.keySet()) {
dbo.put(k, (field.get(k).equals(Order.ASC) ? 1 : -1));
}
return dbo;
}
}package com.x.mongodb;
import com.mongodb.DBCursor;
/**
* 分页,排序处理
* @author java2000_wl
* @version 1.0
*/
public interface CursorPreparer {
DBCursor prepare(DBCursor cursor);
}package com.x.mongodb;
import com.mongodb.DBObject;
/**
* 分页,排序对象
* @author java2000_wl
* @version 1.0
*/
public class CursorObject {
private int skip;
private int limit;
private Sort sort;
public CursorObject skip(int skip) {
this.skip = skip;
return this;
}
public CursorObject limit(int limit) {
this.limit = limit;
return this;
}
public int getSkip() {
return skip;
}
public int getLimit() {
return limit;
}
public Sort sort() {
if (this.sort == null) {
this.sort = new Sort();
}
return this.sort;
}
public DBObject getSortObject() {
if (this.sort == null) {
return null;
}
return this.sort.getSortObject();
}
}
页:
[1]