MongoDB简单测试

pom.xml添加:

<!-- mongo -->
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
			<version>3.0.2</version>
		</dependency>




import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;


public class MongoDbWithJavaTest {

    public static void main(String[] args) {
        MongoClient mongo = null;
        try {
            // connect to mongodb
            mongo = new MongoClient("localhost", 27017);

            // list all databases
            listDatabases(mongo);

            // get database named "test"
            MongoDatabase testDatabase = mongo.getDatabase("test");

            // list all collections(tables)
            listCollections(testDatabase);

            MongoCollection<Document> userCollection = testDatabase.getCollection("user");

            // list all documents in user
            listAllDocuments(userCollection);

            // insert new document
//            insert(userCollection);

            // list all documents in user after insert
//            listAllDocuments(userCollection);
//            listAllSpecifiedDocumentFields(userCollection);

            // list document with given filter
//            listDocumentWithFilter(userCollection);
//            
//            listDocumentWithFilterAndInReverseOrder(userCollection);
            
            // update document
//            updateOneDocument(userCollection);
            updateAllDocument(userCollection);
//            
//            deleteOne(userCollection);
//            deleteMany(userCollection);
//            
            listAllDocuments(userCollection);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mongo != null) {
                mongo.close();
                mongo = null;
            }
        }
    }
    
    public static void deleteOne(MongoCollection<Document> collection) {
        System.out.println("delete one records age less than 24");
        collection.deleteOne(Filters.lt("age", 24));
    }
    
    public static void deleteMany(MongoCollection<Document> collection) {
        System.out.println("delete all records age less than 24");
        collection.deleteMany(Filters.lt("age", 24));
    }

    public static void updateOneDocument(MongoCollection<Document> collection) {
        System.out.println("updateDocument : update one records that named 'dreamoftch' to 'ZhangSan'");
        collection.updateOne(Filters.eq("name", "dreamoftch"), new Document("$set", new Document("name", "ZhangSan")));
    }
    
    public static void updateAllDocument(MongoCollection<Document> collection) {
        System.out.println("updateDocument : update all records that named 'dreamoftch' to 'ZhangSan'");
        collection.updateMany(Filters.eq("name", "dreamoftch"), new Document("$set", new Document("name", "ZhangSan")));
    }

    public static void listDatabases(MongoClient mongo) {
        // list all databases
        MongoIterable<String> allDatabases = mongo.listDatabaseNames();
        for (String db : allDatabases) {
            System.out.println("Database name: " + db);
        }
    }

    public static void listCollections(MongoDatabase database) {
        // list all databases
        MongoIterable<String> allCollections = database.listCollectionNames();
        for (String collection : allCollections) {
            System.out.println("Collection name: " + collection);
        }
    }
    
    public static void listAllDocuments(MongoCollection<Document> collection) {
        System.out.println("begin get all document >>>>>>");
        for (Document document : collection.find()) {
            System.out.println(document);
        }
        System.out.println("finish get all document >>>>>>");
    }
    
    public static void listAllSpecifiedDocumentFields(MongoCollection<Document> collection) {
        System.out.println("begin get all document(exclude '_id') >>>>>>");
        for (Document document : collection.find().projection(Projections.exclude("_id"))) {
            System.out.println(document);
        }
        System.out.println("finish get all document(exclude '_id') >>>>>>");
    }
    
    public static void insert(MongoCollection<Document> collection){
        List<Document> documents = new ArrayList<Document>();
        for (int i = 0; i < 10; i++) {
            documents.add(new Document("name", "dreamoftch").append("age", (20+i)).append("createdDate", new Date()));
        }
        collection.insertMany(documents);
    }
    
    public static void listDocumentWithFilter(MongoCollection<Document> collection) {
        System.out.println("begin get document(name: dreamoftch, age > 25) >>>>>>");
        for (Document document : collection.find(Filters.and(Filters.eq("name", "dreamoftch"), Filters.gt("age", 25)))) {
            System.out.println(document);
        }
        System.out.println("finish get document(name: dreamoftch, age > 25) >>>>>>");
    }
    
    public static void listDocumentWithFilterAndInReverseOrder(MongoCollection<Document> collection) {
        System.out.println("begin get document(name: dreamoftch, age > 25) >>>>>>");
        for (Document document : collection.find(Filters.and(Filters.eq("name", "dreamoftch"), Filters.gt("age", 25))).sort(Sorts.descending("age"))) {
            System.out.println(document);
        }
        System.out.println("finish get document(name: dreamoftch, age > 25) >>>>>>");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值