日期:2014-05-16  浏览次数:20406 次

mongodb 常用方法

package com.mongodb;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;


import org.bson.types.ObjectId;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;

/**
?* MongoDB Manager
?*
?* @author dhdu@qq.com
?*
?*/
public class DbManager {
?//instance
?private final static DbManager instance = new DbManager();

?//
?private DbManager() {
?}

?//
?private static Mongo m = null;
?//
?private static DB db = null;
?//
?private static DBCollection coll = null;
?//
?private static GridFS gridFS = null;

?//static block for init
?static {
??try {
???m = new Mongo(DbProperties.getInstance().getValue("db.host"),
?????Integer.valueOf(DbProperties.getInstance().getValue(
???????"db.port")));
???db = m.getDB(DbProperties.getInstance().getValue("db.dbname"));
???coll = db.getCollection(DbProperties.getInstance().getValue(
?????"db.collname"));
???gridFS = new GridFS(db);
??} catch (Exception e) {
???e.printStackTrace();
??}

?}
?/**
? *
? * @return
? * @throws Exception
? */
?public static DbManager getInstance() throws Exception {
??return instance;
?}
?/**
? *
? * @return
? */
?public DBCollection getCollection() {
??return coll;
?}

?/**
? *
? * @return
? */
?public GridFS getGridFS() {
??return gridFS;
?}

?/**
? * ------------------------------------
? *
? **/

?/***
? * ---------------------object
? ***/
?
?/**
? *
? * @param map
? * @return
? * @throws Exception
? */
?public Object insert(Map map) throws Exception {
??int n = coll.insert(map2Obj(map)).getN();
??return n;
?}

?/**
? *
? * @param list
? */
?public void insertBatch(List<Map<String, Object>> list) {
??if (list == null || list.isEmpty()) {
???return;
??}

??List<DBObject> listDB = new ArrayList<DBObject>();
??for (int i = 0; i < list.size(); i++) {
???DBObject dbObject = map2Obj(list.get(i));
???listDB.add(dbObject);
??}
??coll.insert(listDB);
?}

?/**
? *
? * @param map
? */
?public void delete(Map<String, Object> map) {
??DBObject obj = map2Obj(map);
??coll.remove(obj);
?}
?
?/**
? *
? *
? */
?public void deleteAll() {
??List <DBObject>rs=getAll();
??if(rs!=null&&!rs.isEmpty())
??{
???for(int i=0;i<rs.size();i++){
????coll.remove(rs.get(i));
???}
??}??
?}
?/**
? *
? * @param list
? */
?public void deleteBatch(List<Map<String, Object>> list) {
??if (list == null || list.isEmpty()) {
???return;
??}
??for (int i = 0; i < list.size(); i++) {
???coll.remove(map2Obj(list.get(i)));
??}
?}

?/**
? *
? * @param map
? * @return
? */
?public long getCount(Map<String, Object> map) {
??return coll.getCount(map2Obj(map));
?}

?/**
? *
? * @param setFields
? * @param whereFields
? */
?public void update(Map<String, Object> setFields,
???Map<String, Object> whereFields) {
??DBObject obj1 = map2Obj(setFields);
??DBObject obj2 = map2Obj(whereFields);
??