insert コマンドは、コレクションに新しいドキュメントを作成するために使用されます。 1 つのドキュメントまたは複数のドキュメントを一度に挿入できます。
構文
insert コマンドの基本的な構文は次のとおりです。
db.collection.insert(
<single document or array of documents>,
{
writeConcern: <document>,
ordered: <boolean>
}
)
パラメーター
| パラメーター | Description |
|---|---|
<single document or array of documents> |
コレクションに挿入するドキュメントまたはドキュメントの配列 |
writeConcern |
(省略可能)書き込みの問題を表すドキュメント。 書き込みの問題は、書き込み操作に対してサーバーから要求された受信確認のレベルを表します |
ordered |
(省略可能) true場合、サーバーは指定された順序でドキュメントを挿入します。
false場合、サーバーは任意の順序でドキュメントを挿入でき、エラーに関係なくすべてのドキュメントを挿入しようとします |
-
<single document or array of documents>: コレクションに挿入するドキュメントまたはドキュメントの配列。 -
writeConcern:随意。 書き込みの問題を表すドキュメント。 書き込みの問題は、書き込み操作のためにサーバーから要求された受信確認のレベルを表します。 -
ordered:随意。true場合、サーバーは指定された順序でドキュメントを挿入します。false場合、サーバーは任意の順序でドキュメントを挿入でき、エラーに関係なくすべてのドキュメントの挿入を試みます。
例
1 つのドキュメントを挿入する
次のコマンドは、StoreData データベースの stores コレクションに 1 つのドキュメントを挿入します。
db.stores.insertOne({
"storeId": "12345",
"name": "Boulder Innovations",
"location": {
"lat": 37.7749,
"lon": -122.4194
},
"staff": {
"totalStaff": {
"fullTime": 15,
"partTime": 10
}
},
"sales": {
"totalSales": 500000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 300000.00
},
{
"categoryName": "Smartphones",
"totalSales": 200000.00
}
]
},
"promotionEvents": [
{
"eventName": "Summer Sale",
"promotionalDates": {
"startDate": "2024-06-01",
"endDate": "2024-06-30"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 15
},
{
"categoryName": "Smartphones",
"discountPercentage": 10
}
]
},
{
"eventName": "Holiday Specials",
"promotionalDates": {
"startDate": "2024-12-01",
"endDate": "2024-12-31"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 20
},
{
"categoryName": "Smartphones",
"discountPercentage": 25
}
]
}
]
})
複数のドキュメントを挿入する
次のコマンドは、stores コレクションにドキュメントの配列を挿入します。
db.stores.insertMany([
{
"storeId": "12346",
"name": "Graphic Design Institute",
"location": {
"lat": 34.0522,
"lon": -118.2437
},
"staff": {
"totalStaff": {
"fullTime": 20,
"partTime": 5
}
},
"sales": {
"totalSales": 750000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 400000.00
},
{
"categoryName": "Smartphones",
"totalSales": 350000.00
}
]
},
"promotionEvents": [
{
"eventName": "Black Friday",
"promotionalDates": {
"startDate": "2024-11-25",
"endDate": "2024-11-30"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 25
},
{
"categoryName": "Smartphones",
"discountPercentage": 30
}
]
}
]
},
{
"storeId": "12347",
"name": "Relecloud",
"location": {
"lat": 40.7128,
"lon": -74.0060
},
"staff": {
"totalStaff": {
"fullTime": 10,
"partTime": 20
}
},
"sales": {
"totalSales": 600000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 350000.00
},
{
"categoryName": "Smartphones",
"totalSales": 250000.00
}
]
},
"promotionEvents": [
{
"eventName": "New Year Sale",
"promotionalDates": {
"startDate": "2024-01-01",
"endDate": "2024-01-07"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 10
},
{
"categoryName": "Smartphones",
"discountPercentage": 15
}
]
}
]
}
])
_id フィールドの値の指定
_id フィールドが指定されていない場合、サーバーはドキュメントの一意の ObjectId() 値を自動生成します。 ドキュメントで _id フィールドを指定する場合は、コレクション内のすべてのドキュメントでグローバルに一意の値にする必要があります。
_id フィールドの重複値を指定すると、サーバーによって重複キー違反エラーがスローされます。
{
"WriteErrors": [
{
"WriteError": {
"err": {
"index": 0,
"code": 11000,
"errmsg": "Duplicate key violation on the requested collection: Index '_id_'",
"errInfo": "undefined",
"op": {
"testField": "testValue",
"_id": "1"
}
}
}
}
]
}
複数のドキュメントを順番に挿入する
一括で挿入されるドキュメントは、"ordered" を指定するときに順番に挿入できます: true
db.stores.insertMany([
{
"_id": "123456",
"storeId": "123456",
"name": "Graphic Design Institute",
"location": {
"lat": 34.0522,
"lon": -118.2437
},
"staff": {
"totalStaff": {
"fullTime": 20,
"partTime": 5
}
},
"sales": {
"totalSales": 750000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 400000.00
},
{
"categoryName": "Smartphones",
"totalSales": 350000.00
}
]
},
"promotionEvents": [
{
"eventName": "Black Friday",
"promotionalDates": {
"startDate": "2024-11-25",
"endDate": "2024-11-30"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 25
},
{
"categoryName": "Smartphones",
"discountPercentage": 30
}
]
}
]
},
{
"_id": "234567",
"storeId": "234567",
"name": "Relecloud",
"location": {
"lat": 40.7128,
"lon": -74.0060
},
"staff": {
"totalStaff": {
"fullTime": 10,
"partTime": 20
}
},
"sales": {
"totalSales": 600000.00,
"salesByCategory": [
{
"categoryName": "Laptops",
"totalSales": 350000.00
},
{
"categoryName": "Smartphones",
"totalSales": 250000.00
}
]
},
"promotionEvents": [
{
"eventName": "New Year Sale",
"promotionalDates": {
"startDate": "2024-01-01",
"endDate": "2024-01-07"
},
"discounts": [
{
"categoryName": "Laptops",
"discountPercentage": 10
},
{
"categoryName": "Smartphones",
"discountPercentage": 15
}
]
}
]
}
], "ordered": true)
順序付けされた insert コマンドは、ドキュメントが挿入された順序を確認する応答を返します。
{
"acknowledged": true,
"insertedIds": {
"0": "123456",
"1": "234567"
}
}