日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

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

 

概述

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

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

數據庫初始化

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

mongo --host localhost --port 27017

建立一個數據庫testDb和一個populations集合:

use testDb;
db.createCollection(populations);

使用insertMany方法將一些樣本數據添加到集合中:

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
}]);

執行上述insertMany查詢將返回以下文檔:

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

使用MongoDB Shell查詢

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

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進行批量操作

首先創建一個MongoClient連接:

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

使用Java代碼實現相同的批量操作:

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);

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

結論

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

分享到:
標簽:MongoDB
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

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

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定