mangodb 连接java

1.启动

a)mongod --dbpath F:\64bit\mongodb-win32-x86_64-2008plus-ssl-3.2.13-17-gd918f77\db

b)mongo 127.0.0.1:27017

2.java操作mongodb

package com.sishuok.mongotest;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.util.JSON;

public class TestQ {

    // mongdb连接
    private MongoClient client = null;
    // 创建数据库
    private DB db = null;
    // 创建集合,相当于表
    private DBCollection collection = null;

    @Before
    public void before() {
        try {
            client = new MongoClient("127.0.0.1", 27017);
            db = client.getDB("mydb");
            collection = db.getCollection("test");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    @After
    public void after() {
        client.close();
    }

    /**
     * 添加数据1种
     */
    @Test
    public void insert1() {
        BasicDBObject object=new BasicDBObject();
        object.put("qiu", "lao");
        object.put("zz", "zzz");
        collection.insert(object);
    }
    
    /**
     * 添加数据2种
     */
    @Test
    public void insert2() {
        BasicDBObject object=new BasicDBObject();
        object.append("zhang", "hhhhh");
        collection.insert(object);
    }
    
    /**
     * 添加数据3种
     */
    @Test
    public void insert3() {
        BasicDBObjectBuilder s=BasicDBObjectBuilder.start().add("name", "qiushuzhao").add("sex", "man").add("age", 23);
        collection.insert(s.get());
    }
    
    /**
     * 添加数据4种
     */
    @Test
    public void insert4() {
        String json="{'name':'zhang','age':'21','sex':'man'}";
        DBObject object=(DBObject) JSON.parse(json);
        collection.insert(object);
    }
    
    /**
     * 删除数据
     */
    @Test
    public void delete() {
        collection.remove(new BasicDBObject("name","lao"));
    }
    
    /**
     * 修改数据
     */
    @Test
    public void update1() {
        String json="{'aa':'lao','dd':'dd','dddf':'ftg'}";//要修改内容
        DBObject object=(DBObject) JSON.parse(json);
        collection.update(new BasicDBObject("zhang","hhhhh"),object);//修改条件
    }
    
    /**
     * 修改数据
     */
    @Test
    public void update2() {
        collection.update(new BasicDBObject("name","zhang"),new BasicDBObject("$set",new BasicDBObject("age","45")));
    }
    
    
    /**
     * 查询数据(查询一个,第一个)
     */
    @Test
    public void select1() {
        DBObject d=collection.findOne();
        System.out.println(d);
    }
    
    
    /**
     * 查询集合
     */
    @Test
    public List<UserModel> select2() {
        DBCursor d=collection.find();
        List<UserModel> list=new ArrayList<UserModel>();
        UserModel model=null;
        while (d.hasNext()) {
            DBObject oo=d.next();
            model=new UserModel();
            model.setName((String)oo.get("name"));
            model.setUserId((Double)oo.get("id")+"");
        }
        return list;
    }
    

}
//查询条件
/**
     * 1<id<100
     * @throws UnknownHostException
     */
    public static void select1() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref = new BasicDBObject().append("id",new BasicDBObject().append(QueryOperators.GT, 1).append(QueryOperators.LT, 100));
        DBObject findOne = users.findOne(ref);
        System.out.println(findOne);
    }
    
    /**
     * id=2 and name=22
     * @throws UnknownHostException
     */
    public static void select2() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref = new BasicDBObject().append("id","2").append("name", "22");
        DBObject findOne = users.findOne(ref);
        System.out.println(findOne);
    }
    
    /**
     * name !="ggggggggggggg"
     * @throws UnknownHostException
     */
    public static void select3() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref = new BasicDBObject().append("name",new BasicDBObject().append(QueryOperators.NE, "ggggggggggggg"));
        DBObject findOne = users.findOne(ref);
        System.out.println(findOne);
    }
    
    /**
     * age in (55)
     * @throws UnknownHostException
     */
    public static void select4() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref = new BasicDBObject().append("age",new BasicDBObject().append(QueryOperators.IN, new double[]{55}));
        DBObject findOne = users.findOne(ref);
        System.out.println(findOne);
    }
    
    /**
     * age not in(55)
     * @throws UnknownHostException
     */
    public static void select6() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref = new BasicDBObject().append("age",new BasicDBObject().append(QueryOperators.NIN, new double[]{55}));
        DBObject findOne = users.findOne(ref);
        System.out.println(findOne);
    }
    
    /**
     * age=33 or age=55
     * @throws UnknownHostException
     */
    public static void select7() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref = new BasicDBObject().append(QueryOperators.OR, new BasicDBObject[]{new BasicDBObject().append("age",55),new BasicDBObject().append("age",33)});
        DBObject findOne = users.findOne(ref);
        System.out.println(findOne);
    }
//    
    
    /**
     * select distinct name from table;
     * @throws UnknownHostException
     */
    public static void select8() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        List distinct = users.distinct("name");
    }
    /**
     * 排序1升序-1降序
     * @throws UnknownHostException
     */
    public static void select9() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBObject ref=new BasicDBObject().append("name","22");
        DBCursor sort = users.find(ref).sort(new BasicDBObject().append("id",1));
        
    }
    
    /**
     * 查询0条以后的数据
     * @throws UnknownHostException
     */
    public static void select10() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBCursor sort = users.find().skip(0);
        while(sort.hasNext()){
            System.out.println(sort.next().get("name"));
        }
    }
    
    /**
     * 查询前2条数据
     * @throws UnknownHostException
     */
    public static void select11() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBCursor sort = users.find().limit(2);
        while(sort.hasNext()){
            System.out.println(sort.next().get("name"));
        }
    }
    
    /**
     * 查询2-3之间数据
     * @throws UnknownHostException
     */
    public static void select12() throws UnknownHostException {
        MongoClient client = new MongoClient("127.0.0.1", 27017);
        DB db = client.getDB("mydb");
        DBCollection users = db.getCollection("test");
        DBCursor sort = users.find().limit(2).skip(3);
        while(sort.hasNext()){
            System.out.println(sort.next().get("name"));
        }
    }

//修改

 对的

posted on 2017-05-22 17:56  老邱2  阅读(142)  评论(0)    收藏  举报

导航