码迷,mamicode.com
首页 > 数据库 > 详细

Java连接MongoDB样例

时间:2016-06-28 22:12:23      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

package com.moonlit.example;

import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteOperation;
import com.mongodb.BulkWriteResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongodbTest {
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mydb");
        DBCollection coll = db.getCollection("testCollection");
        
        // insert a document
        BasicDBObject doc = new BasicDBObject("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("info", new BasicDBObject("x", 123).append("y", 456));
        coll.insert(doc);
        
        // find the first document in a collection
        DBObject myDoc = coll.findOne();
        System.out.println(myDoc);
        
        // adding multiple documents
        for (int i = 1; i <= 100; i ++) {
            coll.insert(new BasicDBObject("i", i));
        }
        
        // counting documents in a collection
        System.out.println(coll.getCount());
        
        // using a cursor to get all collection
        DBCursor cursor = coll.find();
        try {
            while (cursor.hasNext())
                System.out.println(cursor.next());
        } finally {
            cursor.close();
        }
        
        // get a single document with a guery
        BasicDBObject query = new BasicDBObject("i", 23);
        cursor = coll.find(query);
        try {
            while (cursor.hasNext()) 
                System.out.println(cursor.next());
        } finally {
            cursor.close();
        }
        
        query = new BasicDBObject("j", new BasicDBObject("$ne", 93))
                .append("i", new BasicDBObject("$gt", 90));
        cursor = coll.find(query);
        try {
            while (cursor.hasNext())
                System.out.println(cursor.next());
        } finally {
            cursor.close();
        }
        
        query = new BasicDBObject("i", new BasicDBObject("$ne", 93).append("$gt", 90));
        
        cursor = coll.find(query);
        try {
            while (cursor.hasNext())
                System.out.println(cursor.next());
        } finally {
            cursor.close();
        }

//        // delete all 
//        cursor = coll.find();
//        try {
//            while (cursor.hasNext())
//                coll.remove(cursor.next());
//        } finally {
//            cursor.close();
//        }
//        
//        // count
//        System.out.println(coll.getCount());
        
//        BulkWriteOperation builder = coll.initializeOrderedBulkOperation();
//        builder.insert(new BasicDBObject("_id", 1));
//        builder.insert(new BasicDBObject("_id", 2));
//        builder.insert(new BasicDBObject("_id", 3));
        
//        builder.find(new BasicDBObject("_id", 1)).updateOne(new BasicDBObject("$set", new BasicDBObject("x", 123)));
//        builder.find(new BasicDBObject("_id", 2)).remove();
//        builder.find(new BasicDBObject("_id", 3)).replaceOne(new BasicDBObject("_id", 3).append("x", 4));
        
//        BulkWriteResult result = builder.execute();
        
        
    }
}

 

Java连接MongoDB样例

标签:

原文地址:http://www.cnblogs.com/moonlightpoet/p/5625073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!