mongo Dao层 增删改查方法
来源:互联网 发布:ipad4软件更新打不开 编辑:程序博客网 时间:2024/05/01 21:34
//比如调用的时 调用MongoOpt.findOne(db,xxx)
public class MongoOpt {
public static boolean findTorF(DB db, String collName, DBObject query) {
if (db == null || collName == null || collName.trim().isEmpty()) {
return false;
}
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(query);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while (cursor.hasNext()) {
DBObject doc = cursor.next();
if (doc == null) {
return false;
}
cursor.close();
return true;
}
return false;
}
public static DBObject findOne(DB db, String collName, DBObject query) {
DBObject obj = null;
if (db == null || collName == null || collName.trim().isEmpty()) {
return obj;
}
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(query);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while (cursor.hasNext()) {
DBObject doc = cursor.next();
if (doc == null) {
continue;
}
obj = doc;
break;
}
cursor.close();
return obj;
}
public static DBObject findOne(DB db, String collName, DBObject query, DBObject sort) {
DBObject obj = null;
if (db == null || collName == null || collName.trim().isEmpty()) {
return obj;
}
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(query).sort(sort);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while (cursor.hasNext()) {
DBObject doc = cursor.next();
if (doc == null) {
continue;
}
obj = doc;
break;
}
cursor.close();
return obj;
}
public static List<DBObject> find(DB db, String collName, DBObject query, DBObject sort) {
List<DBObject> docList = new ArrayList<DBObject>();
if (db == null || collName == null || collName.trim().isEmpty()) {
return docList;
}
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(query).sort(sort);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while (cursor.hasNext()) {
DBObject doc = cursor.next();
if (doc == null) {
continue;
}
docList.add(doc);
}
cursor.close();
return docList;
}
public static List<DBObject> find(DB db, String collName, DBObject query, DBObject sort, int nLimit) {
List<DBObject> docList = new ArrayList<DBObject>();
if (db == null || collName == null || collName.trim().isEmpty()) {
return docList;
}
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(query).sort(sort).limit(nLimit);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while (cursor.hasNext()) {
DBObject doc = cursor.next();
if (doc == null) {
continue;
}
docList.add(doc);
}
cursor.close();
return docList;
}
public static List<DBObject> find(DB db, String collName, DBObject query, DBObject sort, int nSkip, int nLimit) {
List<DBObject> docList = new ArrayList<DBObject>();
if (db == null || collName == null || collName.trim().isEmpty()) {
return docList;
}
DBCollection coll = db.getCollection(collName);
DBCursor cursor = coll.find(query).sort(sort).skip(nSkip).limit(nLimit);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
while (cursor.hasNext()) {
DBObject doc = cursor.next();
if (doc == null) {
continue;
}
docList.add(doc);
}
cursor.close();
return docList;
}
public static boolean insert(DB db, String collName, DBObject docParam) {
boolean b = false;
if (db == null || collName == null || collName.trim().isEmpty()) {
return b;
}
DBCollection coll = db.getCollection(collName);
WriteResult wr = coll.insert(docParam);
if (wr.getLastError().getDouble("ok") > 0) {
b = true;
}
return b;
}
public static boolean insertMulti(DB db, String collName, List<DBObject> docListParam) {
boolean b = false;
if (db == null || collName == null || collName.trim().isEmpty()) {
return b;
}
DBCollection coll = db.getCollection(collName);
WriteResult wr = coll.insert(docListParam);
if (wr.getLastError().getDouble("ok") > 0) {
b = true;
}
return b;
}
public static boolean update(DB db, String collName, DBObject query, DBObject docParam) {
boolean b = false;
if (db == null || collName == null || collName.trim().isEmpty()) {
return b;
}
DBCollection coll = db.getCollection(collName);
DBObject object = new BasicDBObject();
object.put("$set", docParam);
WriteResult wr = coll.update(query, object);
if (wr.getLastError().getDouble("ok") > 0) {
b = true;
}
return b;
}
public static boolean updateMulti(DB db, String collName, DBObject query, DBObject docParam) {
boolean b = false;
if (db == null || collName == null || collName.trim().isEmpty()) {
return b;
}
DBCollection coll = db.getCollection(collName);
DBObject object = new BasicDBObject();
object.put("$set", docParam);
WriteResult wr = coll.updateMulti(query, object);
if (wr.getLastError().getDouble("ok") > 0) {
b = true;
}
return b;
}
public static boolean delete(DB db, String collName, DBObject query) {
boolean b = false;
if (db == null || collName == null || collName.trim().isEmpty()) {
return b;
}
DBCollection coll = db.getCollection(collName);
WriteResult wr = coll.remove(query);
if (wr.getLastError().getDouble("ok") > 0) {
b = true;
}
return b;
}
}
0 0
- mongo Dao层 增删改查方法
- dao实现层通用增删改查方法
- dao接口层抽取增删改查方法
- dao层各种增删改查封装成对象方法 以student表 操作
- Mybatis - Mybatis 增删改查之dao层实现
- 常用的增删改查的实现之dao层
- nosql(mongo)增删改查
- mongo 增删改查操作
- mongo客户端mongo VUE增删改查
- mongo客户端mongo VUE增删改查
- mongo客户端mongo VUE增删改查
- mongo客户端mongo VUE增删改查
- mongo客户端mongo VUE增删改查
- mongo客户端mongo VUE增删改查
- yii DAO增删改查
- 万能 dao 增删改查一个方法搞定
- Mybatis原生dao开发方法实现增删改查
- mongo 语法 增删改查 - 群叔
- RCC 2014 Warmup (Div. 2) D. Cunning Gena
- Tomcat配置一个ip绑定多个域名
- 让我进公司的人居然是露珠的直属上司
- goodix触摸屏驱动调试(1)
- 计算几何模板——不断更新
- mongo Dao层 增删改查方法
- vnc图形界面配置
- 保存网格(mesh)到磁盘上
- Oracle中null的使用详解
- c++的this指针和对象数组
- ERDAS IMAGINE 2013裁剪影像方法
- 电脑小技巧70个
- Android Uri、UriMatcher、ContentUris详解
- 获取农历节日.节气