mongodb和java的简单链接

先导入mongo的jar包.MongoDB.java

package com.util;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

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;

/**
 * @author nidayu
 * @date 2015-1-4
 * @Description xxx
 * @version V1.0
 */
public class MongoDB {

	static Mongo connection;
	static DB db;

	public MongoDB(String databaseName) throws UnknownHostException {
		connection = new Mongo("127.0.0.1:27017");
		db = connection.getDB(databaseName);
	}

	public static void main(String[] args) throws UnknownHostException {
		// 连接数据库
		MongoDB mongodb = new MongoDB("ndy");
		// 创建集合
		String collName = "javadb";
		mongodb.createCollection(collName);
		// 插入一条数据
		DBObject obj = new BasicDBObject();
		obj.put("name", "hello");
		obj.put("age", "5");
		obj.put("email", "dayu.ni@quantgroup.cn");
		List<String> books = new ArrayList<String>();
		books.add("EXTJS");
		books.add("HBase");
		obj.put("books", books);
		mongodb.insert(obj, collName);
		// 批量插入数据
		List<DBObject> dbos = new ArrayList<DBObject>();
		DBObject dbo1 = new BasicDBObject("name", "aaa");
		DBObject dbo2 = new BasicDBObject("name", "bbb");
		dbos.add(dbo1);
		dbos.add(dbo2);
		mongodb.insertBatch(dbos, collName);
		// 根据ID删除数据
		int count = mongodb.deleteById("54a94f18bb8b785e749b5c2b", collName);
		System.out.println(count);
		// 根据条件进行删除
		DBObject aaa = new BasicDBObject("name", "aaa");
		mongodb.deleteByDbs(aaa, collName);
		// 更新操作
		DBObject update = new BasicDBObject();
		update.put("$set", new BasicDBObject("name", "aaa"));
		System.out.println(mongodb.update(new BasicDBObject(), update, false, true, collName));
		// 查询某些列
		DBObject keys = new BasicDBObject();
		keys.put("_id", false);
		keys.put("name", true);
		DBCursor cur = mongodb.find(null, keys, collName);
		while (cur.hasNext()) {
			DBObject dbo = cur.next();
			System.out.println(dbo.get("name"));
		}
		// 关闭连接
		connection.close();
	}

	// 创建一个集合
	public void createCollection(String collection) {
		DBObject dbs = new BasicDBObject();
		db.createCollection(collection, dbs);
	}

	// 插入一条数据
	public void insert(DBObject obj, String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		dbcoll.insert(obj);
	}

	// 批量插入数据
	public void insertBatch(List<DBObject> dbses, String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		dbcoll.insert(dbses);
	}

	// 删除数据
	public int deleteById(String id, String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		int count = dbcoll.remove(new BasicDBObject("_id", new ObjectId(id))).getN();
		return count;
	}

	// 根据条件进行删除
	public int deleteByDbs(DBObject obj, String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		int count = dbcoll.remove(obj).getN();
		return count;
	}

	// 更新数据
	private int update(BasicDBObject basicDBObject, DBObject update, boolean b, boolean c, String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		int count = dbcoll.update(basicDBObject, update, b, c).getN();
		return count;
	}

	// 查询数据
	private DBCursor find(DBObject object, DBObject keys, String collectionName) {
		DBCollection dbcoll = db.getCollection(collectionName);
		DBCursor cur = dbcoll.find(object, keys);
		return cur;
		// 分页
		// return cur.limit(5).skip(0);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值