❶ java對所有mongodb表進行增刪改查表名怎麼設置
一、MongoDB資料庫參數配置
1、推薦使用mongodb.cfg.properties配置,則在構造MongoDBService對象的時候只需調用無參構造方法即可自動完成配置。
MongoDBServiceImpl.java
public class MongoDBServiceImpl implements MongoDBService {private String dbName;private String collName;private DB db;//有參構造方法,指定資料庫名與集合名public MongoDBServiceImpl(String dbName, String collName) {this.dbName = dbName;this.collName = collName;try {db = getDb();} catch (Throwable e) {e.printStackTrace();}}//無參構造方法,返回配置文件配置的資料庫對象引用,如果配置文件中沒有設置則返回默認資料庫對象引用public MongoDBServiceImpl() {getDb();}/** 獲取資料庫對象,3種情況(優先順序從高到低):*1、構造方法指定2、配置文件指定3、默認資料庫*(情況2、3在MongoDButil中設置)*/public DB getDb() {if (this.db == null) {if (this.dbName == null) {this.db = MongoDBUtil.getDB();} else {this.db = MongoDBUtil.getDBByName(this.dbName);}}return this.db;}/** 獲取集合對象,3種情況(優先順序從高到低):*1、構造方法指定2、配置文件指定3、默認資料庫*(情況2、3在MongoDButil中設置)*/public DBCollection getCollection() {if(this.collName != null){return db.getCollection(this.collName);}else {return MongoDBUtil.getDBCollection();}}public DBObject map2Obj(Map<string, object=""> map) {DBObject obj = new BasicDBObject();if (map.containsKey("class") && map.get("class") instanceof Class)map.remove("class");obj.putAll(map);return obj;}//插入數據public void insert(DBObject obj) {getCollection().insert(obj);}//插入多條數據public void insertBatch(List<dbobject> list) {if (list == null || list.isEmpty()) {return;}List<dbobject> listDB = new ArrayList<dbobject>();for (int i = 0; i < list.size(); i++) {listDB.add(list.get(i));}getCollection().insert(listDB);}//刪除數據public void delete(DBObject obj) {getCollection().remove(obj);}//刪除多條數據public void deleteBatch(List<dbobject> list) {if (list == null || list.isEmpty()) {return;}for (int i = 0; i < list.size(); i++) {getCollection().remove(list.get(i));}}//獲取集合中的數據數量public long getCollectionCount() {return getCollection().getCount();}//查找符合條件的數據數量public long getCount(DBObject obj) {if (obj != null)return getCollection().getCount(obj);return getCollectionCount();}//查找符合條件的數據public List<dbobject> find(DBObject obj) {DBCursor cur = getCollection().find(obj);return DBCursor2list(cur);}//查找符合條件的數據並排序@Overridepublic List<dbobject> find(DBObject query, DBObject sort) {DBCursor cur;if (query != null) {cur = getCollection().find(query);} else {cur = getCollection().find();}if (sort != null) {cur.sort(sort);}return DBCursor2list(cur);}//查找符合條件的數據並排序,規定數據個數@Overridepublic List<dbobject> find(DBObject query, DBObject sort, int start,int limit) {DBCursor cur;if (query != null) {cur = getCollection().find(query);} else {cur = getCollection().find();}if (sort != null) {cur.sort(sort);}if (start == 0) {cur.batchSize(limit);} else {cur.skip(start).limit(limit);}return DBCursor2list(cur);}//將DBCursor轉化為list<dbobject>private List<dbobject> DBCursor2list(DBCursor cur) {List<dbobject> list = new ArrayList<dbobject>();if (cur != null) {list = cur.toArray();}return list;}//更新數據public void update(DBObject setFields, DBObject whereFields) {getCollection().updateMulti(whereFields, setFields);}//查詢集合中所有數據public List<dbobject> findAll() {DBCursor cur = getCollection().find();List<dbobject> list = new ArrayList<dbobject>();if (cur != null) {list = cur.toArray();}return list;}//由ID獲取數據public DBObject getById(String id) {DBObject obj = new BasicDBObject();obj.put("_id", new ObjectId(id));DBObject result = getCollection().findOne(obj);return result;}public String getDbName() {return dbName;}public void setDbName(String dbName) {this.dbName = dbName;this.db = MongoDBUtil.getDBByName(this.dbName);}public String getCollName() {return collName;}public void setCollName(String collName) {this.collName = collName;}@Overridepublic void printListDBObj(List<dbobject> list) {// TODO Auto-generated method stubfor(DBObject dbObject: list){System.out.println(dbObject);}}}</dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></dbobject></string,>MongoDBUtil.java
❷ java來操作mongodb,排序和limit是在客戶端做的嗎
distinct().sort().limit(10)
使用limit命令,至於前後要看你按照什麼排序了,
limit會按照排序返回指定條數給你