mongodb java查询_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 && cursor.getSkip() <=0 && cursor.getSortObject() == null) {

return dbCursor;

}

DBCursor cursorToUse = dbCursor;

if (cursor.getSkip() > 0) {

cursorToUse = cursorToUse.skip(cursor.getSkip());

}

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;

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值