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

mongodb增删改查

闲着没事干,自己用了下mongodb  写了点 mongodb的增删改查 代码如此啊  有注解,自己看吧

package com.xiaochen.test;

import com.mongodb.*;
import com.mongodb.util.JSON;
import org.bson.types.ObjectId;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

public class MongoDB4CRUDTest {
    private Mongo mg = null;
    private DB db;
    private DBCollection users;

    @Before
    public void init() {
        try {
            mg = new Mongo("localhost", 27017);
            //mg = new Mongo("localhost", 27017);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
        //获取temp DB;如果默认没有创建,mongodb会自动创建
        db = mg.getDB("TEST_CRUD");
        //获取users DBCollection;如果默认没有创建,mongodb会自动创建
        users = db.getCollection("users");
    }

    @After
    public void destory() {
        if (mg != null)
            mg.close();
        mg = null;
        db = null;
        users = null;
        System.gc();
    }

    public void print(Object o) {
        System.out.println(o);
    }

    private void queryAll() {
        print("查询users的所有数据:");
        //db游标
        DBCursor cur = users.find();
        while (cur.hasNext()) {
            print(cur.next());
        }
    }

    @Test
    public void add() {
        //先查询所有数据
        queryAll();
        print("count: " + users.count());

        DBObject user = new BasicDBObject();
        user.put("name", "hoojo");
        user.put("age", 24);
        //users.save(user)保存,getN()获取影响行数
        //print(users.save(user).getN());

        //扩展字段,随意添加字段,不影响现有数据
        user.put("sex", "男");
//        print(users.insert(user));

        //添加多条数据,传递Array对象
        print(users.insert(user, new BasicDBObject("name", "tom")));

        //添加List集合
        List<DBObject> list = new ArrayList<>();
        list.add(user);
        DBObject user2 = new BasicDBObject("name", "lucy");
        user.put("age", 22);
        list.add(user2);
        //添加List集合
        print(users.insert(list));

        //查询下数据,看看是否添加成功
        print("count: " + users.count());
        queryAll();
    }

    @Test
    public void remove() {
        queryAll();
        print("删除id = 4de73f7acd812d61b4626a77:" + users.remove(new BasicDBObject("_id", new ObjectId("525abdbbb0c683cf578095ef"))).getN());
        print("remove age >= 24: " + users.remove(new BasicDBObject("age", new BasicDBObject("$gte", 24))).getN());
    }

    @Test
    public void modify() {
        print("修改:" + users.update(new BasicDBObject("_id", new ObjectId("525abdceb0c6510d21f5b429")), new BasicDBObject("age", 99)).getN());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("age", 9999);
        map.put("name", "liuxiaochen");
        map.put("class", "software");
        print("修改:" + users.update(
                new BasicDBObject("_id", new ObjectId("525abdceb0c6510d21f5b429")),
                new BasicDBObject(map),
                true,//如果数据库不存在,是否添加
                false//多条修改
        ).getN());
        print("修改:" + users.update(
                new BasicDBObject("name", "haha"),
                new BasicDBObject("name", "dingding"),
                true,//如果数据库不存在,是否添加
                true//false只修改第一天,true如果有多条就不修改
        ).getN());

        //当数据库不存在就不修改、不添加数据,当多条数据就不