3.0MongoDB對一個collection的常用操作

獲取數據操作: 
MongoDatabase database = mongoClient.getDatabase("testdb");
獲取一個collection:
MongoCollection<Document> collection = database.getCollection("test");

1、插入一條數據:
命令:
db.test.insert(
"name" : "MongoDB""type" : "database""count" : 1"info" : { x : 203, y : 102 } }

);
java:
 Document doc = new Document("name", "MongoDB")
                       .append("type", "database")
                       .append("count", 1)
                       .append("info", new Document("x", 203).append("y", 102));

collection.insertOne(doc);

2、批量插入數據:

List<Document> documents = new ArrayList<Document>();
 Document doc = new Document("name", "MongoDB")
                       .append("type", "database")
                       .append("count", 1)
                       .append("info", new Document("x", 203).append("y", 102));

        for (int i = 0; i < 100; i++) {
            documents.add(doc );
        }
 collection.insertMany(documents);

3、查詢所有記錄
 MongoCursor<Document> cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }

4、查詢符合相等條件的記錄
cursor =collection.find(eq("i", 71))

5、查詢大於、小於、等於、大於等於、小於等於某一條件的記錄,以及查詢某一範圍的記錄
cursor = collection.find(gt("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$gt",50))).iterator();

cursor = collection.find(lt("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$lt",50))).iterator();

cursor = collection.find(gte("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$gte",50))).iterator();

cursor = collection.find(lte("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$lte",50))).iterator();

cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator();
cursor = collection.find(new Document("i",new Document("$lte",50).append("$gt",40))).iterator();

6、將查詢結果放在set中
 Block<Document> printBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
        };
        collection.find(gt("i", 50)).forEach(printBlock);

7、對查詢結果排序
        myDoc = collection.find(exists("i")).sort(descending("i")).first();
或者myDoc = collection.find(exists("i")).sort(new Document("i",-1)).first();
1或者-1表示升序或者降序

8、定製返回的字段
 myDoc = collection.find().projection(excludeId()).first();//不返回id
 myDoc = collection.find().projection(new Document("name",1).append("type",0)).first();//不返回type,返回name

9、更新文檔,MongoDB有很多修改器
更新一個文檔:collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110)));//將i=10的記錄中i設置爲110

更新多個文檔:
        UpdateResult updateResult = collection.updateMany(lt("i", 100),
         new Document("$inc", new Document("i", 100)));//將i<100的記錄i都加上100
        System.out.println(updateResult.getModifiedCount());

10、刪除文檔
 collection.deleteOne(eq("i", 110));
 DeleteResult deleteResult = collection.deleteMany(gte("i", 100));

11、批量操作
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
        writes.add(new InsertOneModel<Document>(new Document("_id", 4)));
        writes.add(new InsertOneModel<Document>(new Document("_id", 5)));
        writes.add(new InsertOneModel<Document>(new Document("_id", 6)));
        writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2))));
        writes.add(new DeleteOneModel<Document>(new Document("_id", 2)));
        writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4)));

        collection.bulkWrite(writes);//順序執行
         collection.bulkWrite(writes,new BulkWriteOptions().ordered(false));//不保證按照順序執行
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章