Docs Menu

Mongo.bulkWrite()

Mongo.bulkWrite(operations, options)

bulkWrite() 한 번의 호출로 여러 데이터베이스 및 컬렉션에 걸쳐 여러 쓰기 (write) 작업을 수행합니다. 이 메서드는 한 번의 호출로 특정 컬렉션에 대한 여러 쓰기 (write) 작업을 수행하는 db.collection.bulkWrite()을 대체합니다.

참고

Mongo.bulkWrite() 는 MongoDB 8.0 이상에서만 사용할 수 있습니다.

다음 구문을 사용하여 현재 Mongo() 인스턴스에서 bulkWrite() 를 호출할 수 있습니다.

db.getMongo().bulkWrite(
[
{
namespace: "<db1.collection1>",
name: "insertOne",
document: { ... }
},
{
namespace: "<db2.collection2>",
name: "replaceOne",
filter: { ... }
}
],
{
ordered: boolean,
verboseResults: boolean,
bypassDocumentValidation: boolean,
let: Document
}
)

다음 예시 와 같이 다른 Mongo 인스턴스 에서 호출할 수도 있습니다.

const otherMongo = Mongo("<other connection string>");
otherMongo.bulkWrite([{ namespace: "<db.collection>", ... }]);

bulkWrite() 두 개의 매개변수를 허용합니다:

Parameter
유형
설명

operations

문서 배열

쓰기 (write) 작업의 배열 을 정의합니다. 배열 의 각 문서 실행하려는 쓰기 (write) 작업을 나타냅니다.

options

문서

작업에 대한 옵션 을 정의합니다.

operations 의 문서 는 다음 6가지 작업 중 하나를 나타낼 수 있습니다.

  • insertOne

  • 하나 교체

  • UpdateOne

  • 업데이트

  • deleteOne

  • deleteMany

다음 섹션에서는 각 작업을 나타내는 문서에 사용해야 하는 구문에 대해 설명합니다.

{
namespace: '<db.collection>',
name: 'insertOne',
document: Document
}
필드
유형
설명

namespace

문자열

문서 삽입할 데이터베이스 및 컬렉션 입니다.

name

문자열

작업. 하나의 삽입 작업을 수행하려면 "insertOne"(으)로 설정하세요.

document

문서

삽입하려는 문서 입니다.

참고

삽입할 문서 에 _id 필드 지정하지 않으면 mongosh 가 자동으로 _id를 생성합니다.

{
namespace: '<db>.<collection>',
name: 'updateOne' | 'updateMany',
filter: Document,
update: Document | Document[],
arrayFilters?: Document[],
hint?: Document | string,
collation?: Document,
upsert?: boolean
}
필드
유형
설명

namespace

문자열

문서를 업데이트 하려는 데이터베이스 및 컬렉션 입니다.

name

문자열

작업. 1개의 업데이트 작업을 수행하려면 "updateOne"(으)로 설정하세요. 업데이트 다수 작업의 경우 "updateMany" 로 설정합니다.

filter

문서

업데이트 하려는 하나 이상의 문서와 일치하는 필터하다 입니다. 1번 업데이트 작업 중에 mongosh 는 일치하는 첫 번째 문서 만 업데이트합니다. 다수 업데이트 작업 중에 mongosh 는 필터하다 와 일치하는 모든 문서를 업데이트합니다.

update

문서

수행할 업데이트 입니다.

arrayFilters

문서 배열

(선택 사항) 배열 값 필드 업데이트 경우 arrayFilters 은 업데이트 적용되는 배열 요소를 지정하는 필터 설정하다 입니다.

hint

문서 또는 문자열

(선택 사항) 작업에 사용할 인덱스 .

collation

문서

(선택 사항) 결과를 정렬할 때 사용할 데이터 정렬 입니다.

upsert

부울

필터하다 와 일치하는 문서 없는 경우 MongoDB 새 문서 생성할지 여부를 지정합니다. 기본값은 false입니다.

{
namespace: '<db>.<collection>',
name: 'replaceOne',
filter: Document,
replacement: Document,
hint?: Document | string,
collation?: Document
}
필드
유형
설명

namespace

문자열

문서를 대체하려는 데이터베이스 및 컬렉션 입니다.

name

문자열

작업. 하나의 교체 작업을 수행하려면 "replaceOne" 로 설정합니다.

filter

문서

업데이트 하려는 문서 와 일치하는 필터하다 입니다. 하나 바꾸기 작업 중에 mongosh 은 일치하는 첫 번째 문서 만 대체합니다.

replacement

문서

대체 문서입니다.

hint

문서 또는 문자열

(선택 사항) 작업에 사용할 인덱스 .

collation

문서

(선택 사항) 결과를 정렬할 때 사용할 데이터 정렬 입니다.

{
namespace: '<db>.<collection>',
name: 'deleteOne' | 'deleteMany',
filter: Document,
hint?: Document | string,
collation?: Document
}
필드
유형
설명

namespace

문자열

문서를 삭제 하려는 데이터베이스 및 컬렉션 입니다.

name

문자열

작업. 하나의 삭제 작업을 수행하려면 "deleteOne"(으)로 설정하세요. 다수 삭제 작업을 수행하려면 "deleteMany" 로 설정합니다.

filter

문서

삭제 하려는 문서 와 일치하는 필터하다 입니다. 하나 삭제 작업 중에 mongosh 는 일치하는 첫 번째 문서 만 삭제합니다. 다수 삭제 작업 중에 mongosh 는 일치하는 모든 문서를 삭제합니다.

hint

문서 또는 문자열

(선택 사항) 작업에 사용할 인덱스 .

collation

문서

(선택 사항) 결과를 정렬할 때 사용할 데이터 정렬 입니다.

bulkWrite()와 함께 다음 옵션을 사용할 수 있습니다. 사용하려는 옵션이 포함된 문서 bulkWrite()(으)로 전달할 수 있습니다. 이 문서 선택 사항입니다.

{
ordered?: boolean,
verboseResults?: boolean,
bypassDocumentValidation?: boolean,
let?: Document
}
필드
유형
설명

ordered

부울

(선택 사항) MongoDB 사용자가 제공하는 문서 순서대로 대량 쓰기 (write) 수행함을 나타냅니다. 기본값은 true입니다.

verboseResults

부울

(선택 사항) bulkWrite() 가 자세한 결과를 출력할지 여부를 지정합니다. 기본값은 false입니다.

bypassDocumentValidation

부울

(선택 사항) 쓰기 (write) 작업이 문서 유효성 검사 규칙을 우회할지 여부를 지정합니다. 기본값은 false입니다.

let

문서

bulkWrite() 다음 필드가 있는 객체 반환합니다.

{
acknowledged: boolean,
insertedCount: int,
matchedCount: int,
modifiedCount: int,
deletedCount: int,
upsertedCount: int,
insertResults?: map(int, document),
updateResults?: map(int, document),
deleteResults?: map(int, document)
}
필드
유형
설명

acknowledged

부울

true 서버 승인을 반환하면 false, 그렇지 않으면 입니다.

insertedCount

integer

삽입된 문서 수입니다.

matchedCount

integer

필터하다 와 일치하는 문서 수입니다.

modifiedCount

integer

수정된 문서 수입니다.

deletedCount

integer

삭제된 문서 수입니다.

upsertedCount

integer

업서트된 문서 수입니다.

insertResults

문서에 정수 매핑

선택 사항. 성공적인 각 삽입 작업의 결과를 나타냅니다. 각 작업은 정수 키로 표시되며, 여기에는 작업에 해당하는 정보가 포함된 문서 포함됩니다. 문서에는 다음 필드 포함됩니다.

  • insertedId: ObjectID. 삽입된 문서 의 _id 을(를) 나타냅니다.

updateResults

문서에 정수 매핑

선택 사항. 성공적인 각 업데이트 작업의 결과를 나타냅니다. 각 작업은 정수 키로 표시되며, 여기에는 작업에 해당하는 정보가 포함된 문서 포함됩니다. 문서에는 다음 필드가 포함됩니다.

  • matchedCount: 정수. 일치하는 문서 수를 나타냅니다.

  • modifiedCount: 정수. 수정된 문서 수를 나타냅니다.

  • upsertedId: ObjectID. 업서트된 문서의 _id 를 나타냅니다. 선택 사항.

  • didUpsert: 부울. 문서 업서트된 경우 true, 그렇지 않으면 false 입니다.

deleteResults

문서에 정수 매핑

선택 사항. 성공적인 각 삭제 작업의 결과를 나타냅니다. 각 작업은 정수 키로 표시되며, 여기에는 작업에 해당하는 정보가 포함된 문서 포함됩니다. 문서에는 다음 필드 포함됩니다.

  • deletedCount: 정수. 삭제된 문서 수를 나타냅니다.

mongosh 명령은 다음 작업을 순서대로 수행합니다.

  • db.authors 컬렉션 에 문서 삽입합니다.

  • db.books 컬렉션 에 문서 삽입합니다.

  • 이전 문서 업데이트합니다.

db.getMongo().bulkWrite(
[
{
namespace: 'db.authors',
name: 'insertOne',
document: { name: 'Stephen King' }
},
{
namespace: 'db.books',
name: 'insertOne',
document: { name: 'It' }
},
{
namespace: 'db.books',
name: 'updateOne',
filter: { name: 'it' },
update: { $set: { year: 1986 } }
}
],
{
ordered: true,
bypassDocumentValidation: true
}
)

mongosh 대량 쓰기 (write) 순서대로 수행하고 다음 문서 반환합니다.

{
acknowledged: true,
insertedCount: 2,
matchedCount: 1,
modifiedCount: 1,
deletedCount: 0,
upsertedCount: 0,
insertResults: { '1': { insertedId: ObjectId('67ed8ce8efd926c84cab7945') },
'2': { insertedId: ObjectId('67ed8ce8efd926c84cab7946') } }
updateResults: { '1': { matchedCount: 1, modifiedCount: 1, didUpsert: false } }
}