팁
MongoDB는 대량 쓰기 작업을 수행하기 위한 db.collection.bulkWrite() 메서드도 제공합니다.
설명
Bulk.find.upsert()업데이트 또는 교체 작업에 대해 업서트 옵션을 true로 설정하며 구문은 다음과 같습니다.
Bulk.find(<query>).upsert().update(<update>); Bulk.find(<query>).upsert().updateOne(<update>); Bulk.find(<query>).upsert().replaceOne(<replacement>); upsert옵션을true로 설정한 경우Bulk.find()조건과 일치하는 문서가 없으면 업데이트 또는 교체 작업에서 삽입을 수행합니다. 일치하는 문서가 있는 경우 업데이트 또는 교체 작업을 통해 지정된 업데이트 또는 교체가 수행됩니다.Use
Bulk.find.upsert()with the following write operations:
호환성
이 명령은 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: 클라우드에서의 MongoDB 배포를 위한 완전 관리형 서비스
참고
이 명령은 모든 MongoDB Atlas 클러스터에서 지원됩니다. 모든 명령에 대한 Atlas 지원에 관해 자세히 알아보려면 지원되지 않는 명령을 참조하세요.
행동
The following describe the insert behavior of various write operations when used in conjunction with Bulk.find.upsert().
Insert for Bulk.find.replaceOne()
Bulk.find.replaceOne() 메서드는 필드와 값 쌍만 포함된 교체 문서를 매개변수로 받습니다.
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { item: "abc123" } ).upsert().replaceOne( { item: "abc123", status: "P", points: 100, } ); bulk.execute();
If the replacement operation with the Bulk.find.upsert() option performs an insert, the inserted document is the replacement document. If neither the replacement document nor the query document specifies an _id field, MongoDB adds the _id field:
{ "_id" : ObjectId("52ded3b398ca567f5c97ac9e"), "item" : "abc123", "status" : "P", "points" : 100 }
Insert for Bulk.find.updateOne()
Bulk.find.updateOne() 메서드는 다음 중 하나를 매개변수로 허용합니다.
필드 및 값 쌍만 포함하는 교체 문서(
Bulk.find.replaceOne()과 동일),업데이트 연산자 표현식만 포함하는 업데이트 문서 또는
집계 파이프라인.
필드 및 값 쌍
매개변수가 필드 및 값 쌍만 포함하는 교체 문서인 경우:
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: "P" } ).upsert().updateOne( { item: "TBD", points: 0, inStock: true, status: "I" } ); bulk.execute();
Then, if the update operation with the Bulk.find.upsert() option performs an insert, the inserted document is the replacement document. If neither the replacement document nor the query document specifies an _id field, MongoDB adds the _id field:
{ "_id" : ObjectId("52ded5a898ca567f5c97ac9f"), "item" : "TBD", "points" : 0, "inStock" : true, "status" : "I" }
연산자 표현식 업데이트
매개 변수가 업데이트 연산자 표현식만을 포함하는 업데이트 문서인 경우:
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: "P", item: null } ).upsert().updateOne( { $setOnInsert: { qty: 0, inStock: true }, $set: { points: 0 } } ); bulk.execute();
Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a document with field and values from the query document of the Bulk.find() method and then applies the specified updates from the update document. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:
{ "_id" : ObjectId("5e28d1a1500153bc2872dadd"), "item" : null, "status" : "P", "inStock" : true, "points" : 0, "qty" : 0 }
집계 파이프라인
업데이트 메서드는 집계 파이프라인을 허용할 수 있습니다. 예를 들어 다음과 같은 용도로 사용할 수 있습니다.
$setOnInsert업데이트 연산자 표현식과 다소 유사한 동작을 제공할 수 있는$replaceRoot단계,집계 변수
NOW는 현재 날짜/시간으로 해석되며$currentDate업데이트 연산자 표현식과 유사한 동작을 제공할 수 있습니다.
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { item: "Not Found", status: "P" } ).upsert().updateOne( [ { $replaceRoot: { newRoot: { $mergeObjects: [ { qty: 0, inStock: true }, "$$ROOT" ] } } }, { $set: { points: 0, lastModified: "$$NOW" } } ] ); bulk.execute();
Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a document with field and values from the query document of the Bulk.find() method and then applies the specified aggregation pipeline. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:
{ "_id" : ObjectId("5e28cf1e500153bc2872d49f"), "qty" : 0, "inStock" : true, "item" : "Not Found", "status" : "P", "points" : 0, "lastModified" : ISODate("2020-01-22T22:39:26.789Z") }
Insert for Bulk.find.update()
When using upsert() with the multiple document update method Bulk.find.update(), if no documents match the query condition, the update operation inserts a single document.
Bulk.find.update() 메서드는 다음 중 하나를 매개 변수로 허용합니다.
업데이트 연산자 표현식만 포함하는 업데이트 문서 또는
집계 파이프라인.
연산자 표현식 업데이트
매개 변수가 업데이트 연산자 표현식만을 포함하는 업데이트 문서인 경우:
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { status: "P" } ).upsert().update( { $setOnInsert: { qty: 0, inStock: true }, $set: { status: "I", points: "0" } } ); bulk.execute();
Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a single document with the fields and values from the query document of the Bulk.find() method and then applies the specified update from the update document. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:
{ "_id": ObjectId("52ded81a98ca567f5c97aca1"), "status": "I", "qty": 0, "inStock": true, "points": "0" }
집계 파이프라인
업데이트 메서드는 집계 파이프라인을 허용할 수 있습니다. 예를 들어 다음과 같은 용도로 사용할 수 있습니다.
$setOnInsert업데이트 연산자 표현식과 다소 유사한 동작을 제공할 수 있는$replaceRoot단계,집계 변수
NOW는 현재 날짜/시간으로 해석되며$currentDate업데이트 연산자 표현식과 유사한 동작을 제공할 수 있습니다.NOW값은 파이프라인 전체에서 동일하게 유지됩니다. 집계 변수에 액세스하려면 변수 앞에 이중 달러 기호($$)를 붙이고 따옴표로 묶습니다.
var bulk = db.items.initializeUnorderedBulkOp(); bulk.find( { item: "New Item", status: "P" } ).upsert().update( [ { $replaceRoot: { newRoot: { $mergeObjects: [ { qty: 0, inStock: true }, "$$ROOT" ] } } }, { $set: { points: 0, lastModified: "$$NOW" } } ] ); bulk.execute();
Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a single document with the fields and values from the query document of the Bulk.find() method and then applies the aggregation pipeline. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:
{ "_id" : ObjectId("5e2920a5b4c550aad59d18a1"), "qty" : 0, "inStock" : true, "item" : "New Item", "status" : "P", "points" : 0, "lastModified" : ISODate("2020-01-23T04:27:17.780Z") }