mongoDB的基本操作

来源:互联网 发布:淘宝联盟的返利是店家 编辑:程序博客网 时间:2024/05/16 07:22

基本入门参考:

import com.mongodb.*;import junit.framework.TestCase;import org.bson.types.ObjectId;import org.junit.After;import org.junit.Before;import org.junit.Test;import java.awt.image.DataBuffer;import java.util.*;/** * Created by wzhjdls on 14-12-20. */public class MongoDBTest extends TestCase {    Mongo mongo = null;    DB db = null;    DBCollection user = null;    @Before    public void setUp() throws Exception {        mongo = new Mongo("localhost", 27017);        db = mongo.getDB("test");        user = db.getCollection("user");    }    @After    public void destroy(){        mongo.close();    }    //Query all collections name    public void testGetAllCollections() {        Set<String> collections = db.getCollectionNames();        for (String name : collections) {            System.out.println(name);        }    }    //Query all user informations    @Test    public void testFind() {        testInitTestData();//ini        //Query all data and return DBCusor        DBCursor cursor = user.find();        while (cursor.hasNext()) {            print(cursor.next());        }        //获取数据总条数        int sum = cursor.count();        System.out.println("sum==" + sum);    }    public void testFind1() {        DBCursor cursor = user.find();        while (cursor.hasNext()) {            print(cursor.next());        }    }    @Test    public void testFindm(){//多个条件查询        BasicDBObject basicDBObject = new BasicDBObject();        basicDBObject.put("name","longzhen");        basicDBObject.put("age",20);        DBCursor cursor = user.find(basicDBObject);        while (cursor.hasNext()) {            print(cursor.next());        }        //获取数据总条数        int sum = cursor.count();        System.out.println("sum==" + sum);    }    public void testFindCase1(){        BasicDBObject basicDBObject = new BasicDBObject();        basicDBObject.put("name","longzhen");        basicDBObject.put("age",20);        BasicDBList basicDBList = new BasicDBList();        DBCursor cursor = user.find(basicDBObject);        while (cursor.hasNext()) {            basicDBList.add(cursor.next());            print(cursor.next());        }        //获取数据总条数        int sum = cursor.count();        System.out.println("sum==" + sum);    }    //Query first data    public void testFindOne() {        testInitTestData();        DBObject oneUser = user.findOne();        print(oneUser);    }    //Query conditions    public void testConditionQuery() {        testInitTestData();        //Query id =        DBObject oneUser = user.findOne(new BasicDBObject("name", "zhangguochen"));        print(oneUser);        //Query age= 24        List<DBObject> userList1 = user.find(new BasicDBObject("age", 24)).toArray();        printList(userList1);        System.out.println();        //Query age >=23        List<DBObject> userList2 = user.find(new BasicDBObject("age", new BasicDBObject("$gte", 23))).toArray();        print("----Query age >=23");        printList(userList2);        //Query age <=25        List<DBObject> userList3 = user.find(new BasicDBObject("age", new BasicDBObject("$lte", 25))).toArray();        print("----Query age <=25");        printList(userList3);        //查询age!=25        List<DBObject> userList4 = user.find(new BasicDBObject("age", new BasicDBObject("$ne", 25))).toArray();        print("        find age!=25: ");        printList(userList4);        //查询age in[23,24,27]        List<DBObject> userList5 = user.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.IN, new int[]{23, 24, 27}))).toArray();        print("find agein[23,24,27]: ");        printList(userList5);        /**         * 初//查询age not in[23,24,27]始化测试数据         */        List<DBObject> userList6 = user.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.NIN, new int[]{23, 24, 27}))).toArray();        print("        find age not in[23,24,27]: ");        printList(userList6);        //查询29>age>=20        List<DBObject> userList7 = user.find(new BasicDBObject("age", new BasicDBObject("$gte", 20).append("$lt", 29))).toArray();        print("        find 29>age>=20: ");        printList(userList7);    }    @Test  //Update    public void testQuery1() {//        testInitTestData();        BasicDBObject doc = new BasicDBObject();        doc.put("address", "henan");        BasicDBObject doc1 = new BasicDBObject("home", doc);        List<DBObject> userList = user.find(doc1).toArray();//        userList.get(0).get("age");//        System.out.println(userList.get(0).get("age"));        printList(userList);        BasicDBObject updateDoc = new BasicDBObject("age", 25);        user.update(updateDoc, new BasicDBObject("age", 876)); //这样将更新所有字段        List<DBObject> userList1 = user.find(new BasicDBObject("age", 876)).toArray();        printList(userList1);    }    //Update 局部    @Test    public void testUpdated() {        BasicDBObject doc = new BasicDBObject();        doc.put("$set", new BasicDBObject("age", 233));        BasicDBObject doc1 = new BasicDBObject("name", "longzhen");        List<DBObject> list = user.find(doc1).toArray();        printList(list);        System.out.println();        user.update(doc1, doc, true, false);        List<DBObject> list1 = user.find(doc1).toArray();        printList(list1);        System.out.println();        //remove data        BasicDBObject doc2 = new BasicDBObject("age", 233);        user.remove(doc2);        List<DBObject> list2 = user.find(doc1).toArray();        list2.get(0).get("key");        printList(list2);    }    @Test    public void testQuery3() {        testInitTestData();//        BasicDBObject doc = new BasicDBObject("name", "cuichongfei");        DBCursor cursor = user.find();        while (cursor.hasNext()) {            System.out.println(cursor.next());        }        DBObject oneUser = user.findOne(new BasicDBObject("name", "longzhen"));        print(oneUser);        System.out.println();        //指定字段查询,只查询age和name两个字段        //查询特定字段        DBObject query1 = new BasicDBObject();//要查的条件        query1.put("age", new BasicDBObject("$gt",20));        DBObject field = new BasicDBObject();//要查的哪些字段        field.put("name", true);        field.put("age", true);        List<DBObject> userList10=user.find(query1,field).toArray();        print("        select name,age where age>20");        printList(userList10);    }    @Test    public void testInitTestData() {        DBObject jindazhong = new BasicDBObject();        jindazhong.put("name", "jindazhong");        jindazhong.put("age", 21);        jindazhong.put("interest", new String[]{"hadoop", "mongodb"});        jindazhong.put("wife", "小龙女");        DBObject jindazhongAddress = new BasicDBObject();        jindazhongAddress.put("address", "shanghai");        jindazhong.put("home", jindazhongAddress);        DBObject yangzhi = new BasicDBObject();        yangzhi.put("name", "yangzhi");        yangzhi.put("age", 22);        yangzhi.put("interest", new String[]{"shopping", "sing", "hadoop"});        DBObject yangzhiAddress = new BasicDBObject();        yangzhiAddress.put("address", "hubei");        yangzhi.put("home", yangzhiAddress);        DBObject diaoyouwei = new BasicDBObject();        diaoyouwei.put("name", "diaoyouwei");        diaoyouwei.put("age", 23);        diaoyouwei.put("interest", new String[]{"notejs", "sqoop"});        DBObject diaoyouweiAddress = new BasicDBObject();        diaoyouweiAddress.put("address", "shandong");        diaoyouwei.put("home", diaoyouweiAddress);        DBObject cuichongfei = new BasicDBObject();        cuichongfei.put("name", "cuichongfei");        cuichongfei.put("age", 24);        cuichongfei.put("interest", new String[]{"ebsdi", "dq"});        cuichongfei.put("wife", "凤姐");        DBObject cuichongfeiAddress = new BasicDBObject();        cuichongfeiAddress.put("address", "shanxi");        cuichongfei.put("home", cuichongfeiAddress);        DBObject huanghu = new BasicDBObject();        huanghu.put("name", "huanghu");        huanghu.put("age", 25);        huanghu.put("interest", new String[]{"shopping", "study"});        huanghu.put("wife", "黄蓉");        DBObject huanghuAddress = new BasicDBObject();        huanghuAddress.put("address", "guangdong");        huanghu.put("home", huanghuAddress);        DBObject houchangren = new BasicDBObject();        houchangren.put("name", "houchangren");        houchangren.put("age", 26);        houchangren.put("interest", new String[]{"dota", "dq"});        DBObject houchangrenAddress = new BasicDBObject();        houchangrenAddress.put("address", "shandong");        houchangren.put("home", houchangrenAddress);        DBObject wangjuntao = new BasicDBObject();        wangjuntao.put("name", "wangjuntao");        wangjuntao.put("age", 24);        wangjuntao.put("interest", new String[]{"sport", "study"});        wangjuntao.put("wife", "王语嫣");        DBObject wangjuntaoAddress = new BasicDBObject();        wangjuntaoAddress.put("address", "hebei");        wangjuntao.put("home", wangjuntaoAddress);        DBObject miaojiagui = new BasicDBObject();        miaojiagui.put("name", "miaojiagui");        miaojiagui.put("age", 28);        miaojiagui.put("interest", new String[]{"hadoop", "study", "linux"});        miaojiagui.put("wife", null);        DBObject miaojiaguiAddress = new BasicDBObject();        miaojiaguiAddress.put("address", "未知");        miaojiagui.put("home", miaojiaguiAddress);        DBObject longzhen = new BasicDBObject();        longzhen.put("name", "longzhen");        longzhen.put("age", 20);        Map<String,String> map = new HashMap<String,String>();        map.put("name","chenpu");        map.put("age","21");        Map<String,String> map1 = new HashMap<String,String>();        map1.put("name","zhang");        map1.put("age", "22");        List<Map<String,String>> list= new LinkedList<Map<String,String>>();        list.add(map);        list.add(map1);        longzhen.put("interest", list);        longzhen.put("wife", null);        DBObject longzhenAddress = new BasicDBObject();        longzhenAddress.put("address", "sichuan");        longzhen.put("home", longzhenAddress);        user.insert(jindazhong);        user.insert(yangzhi);        user.insert(diaoyouwei);        user.insert(cuichongfei);        user.insert(huanghu);        user.insert(houchangren);        user.insert(wangjuntao);        user.insert(miaojiagui);        user.insert(longzhen);    }    //delete collections    public void testRemove() {        user.drop();    }    //print data    public void print(Object object) {        System.out.println(object);    }    public void printList(List<DBObject> objectList) {        for (Object object : objectList) {            print(object);        }    }}

0 0
原创粉丝点击