亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

 

概述

MongoDB支持執(zhí)行批量更新和插入操作,允許在一次操作中插入或檢索多個(gè)文檔。通過使用Batch接口,減少客戶端和數(shù)據(jù)庫之間的調(diào)用次數(shù),可以顯著提高數(shù)據(jù)庫訪問性能。

本文將演示如何使用MongoDB Shell和JAVA代碼兩種方式實(shí)現(xiàn)文檔的批量更新。

數(shù)據(jù)庫初始化

首先,我們需要連接到mongo shell:

mongo --host localhost --port 27017

建立一個(gè)數(shù)據(jù)庫testDb和一個(gè)populations集合:

use testDb;
db.createCollection(populations);

使用insertMany方法將一些樣本數(shù)據(jù)添加到集合中:

db.populations.insertMany([
{
    "cityId":1124,
    "cityName":"New York",
    "countryName":"United States",
    "continentName":"North America",
    "population":22
},
{
    "cityId":1125,
    "cityName":"Mexico City",
    "countryName":"Mexico",
    "continentName":"North America",
    "population":25
},
{
    "cityId":1126,
    "cityName":"New Delhi",
    "countryName":"India",
    "continentName":"Asia",
    "population":45
},
{
    "cityId":1134,
    "cityName":"London",
    "countryName":"England",
    "continentName":"Europe",
    "population":32
}]);

執(zhí)行上述insertMany查詢將返回以下文檔:

{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("623575049d55d4e137e477f6"),
        ObjectId("623575049d55d4e137e477f7"),
        ObjectId("623575049d55d4e137e477f8"),
        ObjectId("623575049d55d4e137e477f9")
    ]
}

使用MongoDB Shell查詢

MongoDB的批量操作生成器用于為單個(gè)集合批量構(gòu)建寫操作列表,可以使用insert、update、replace和remove等方法執(zhí)行不同類型的操作:

db.populations.bulkWrite([
    { 
        insertOne :
            { 
                "document" :
                    {
                        "cityId":1128,
                        "cityName":"Kathmandu",
                        "countryName":"Nepal",
                        "continentName":"Asia",
                        "population":12
                    }
            }
    },
    { 
        insertOne :
            { 
                "document" :
                    {
                        "cityId":1130,
                        "cityName":"Mumbai",
                        "countryName":"India",
                        "continentName":"Asia",
                        "population":55
                    }
            }
    },
    { 
        updateOne :
            { 
                "filter" : 
                     { 
                         "cityName": "New Delhi"
                     },
                 "update" : 
                     { 
                         $set : 
                         { 
                             "status" : "High Population"
                         } 
                     }
            }
    },
    { 
        updateMany :
            { 
                "filter" : 
                     { 
                         "cityName": "London"
                     },
                 "update" : 
                     { 
                         $set : 
                         { 
                             "status" : "Low Population"
                         } 
                     }
            }
    },
    { 
        deleteOne :
            { 
                "filter" : 
                    { 
                        "cityName":"Mexico City"
                    } 
            }
    },
    { 
        replaceOne :
            {
                "filter" : 
                    { 
                        "cityName":"New York"
                    },
                 "replacement" : 
                    {
                        "cityId":1124,
                        "cityName":"New York",
                        "countryName":"United States",
                        "continentName":"North America",
                        "population":28
                    }
             }
    }
]);

上述bulkWrite查詢將返回以下文檔:

{
    "acknowledged" : true,
    "deletedCount" : 1,
    "insertedCount" : 2,
    "matchedCount" : 3,
    "upsertedCount" : 0,
    "insertedIds" : 
        {
            "0" : ObjectId("623575f89d55d4e137e477f9"),
            "1" : ObjectId("623575f89d55d4e137e477fa")
        },
    "upsertedIds" : {}
}

使用Java進(jìn)行批量操作

首先創(chuàng)建一個(gè)MongoClient連接:

MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("testDb");
MongoCollection<Document> collection = database.getCollection("populations");

使用Java代碼實(shí)現(xiàn)相同的批量操作:

List<WriteModel<Document>> writeOperations = new ArrayList<WriteModel<Document>>();
writeOperations.add(new InsertOneModel<Document>(new Document("cityId", 1128)
  .Append("cityName", "Kathmandu")
  .append("countryName", "Nepal")
  .append("continentName", "Asia")
  .append("population", 12)));
writeOperations.add(new InsertOneModel<Document>(new Document("cityId", 1130)
  .append("cityName", "Mumbai")
  .append("countryName", "India")
  .append("continentName", "Asia")
  .append("population", 55)));
writeOperations.add(new UpdateOneModel<Document>(new Document("cityName", "New Delhi"),
  new Document("$set", new Document("status", "High Population"))
));
writeOperations.add(new UpdateManyModel<Document>(new Document("cityName", "London"),
  new Document("$set", new Document("status", "Low Population"))
));
writeOperations.add(new DeleteOneModel<Document>(new Document("cityName", "Mexico City")));
writeOperations.add(new ReplaceOneModel<Document>(new Document("cityId", 1124), 
  new Document("cityName", "New York").append("cityName", "United States")
    .append("continentName", "North America")
    .append("population", 28)));
BulkWriteResult bulkWriteResult = collection.bulkWrite(writeOperations);
System.out.println("bulkWriteResult:- " + bulkWriteResult);

首先創(chuàng)建了一個(gè)writeModel列表,將所有不同類型的寫操作添加到一個(gè)更新列表中。此外,我們在查詢中使用了InsertOneModel、UpdateOneModel、UpdateManyModel、DeleteOneModel和ReplaceOneModel。最后,bulkWrite方法一次執(zhí)行了所有的操作。

結(jié)論

MongoDB的Bulk批量操作詳細(xì)資料可以參考:
https://www.mongodb.com/docs/manual/core/bulk-write-operations/

分享到:
標(biāo)簽:MongoDB
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定