Computer >> 컴퓨터 >  >> 프로그램 작성 >> MongoDB

전체 문서를 교체하면서 MongoDB 문서를 어떻게 업데이트합니까?

<시간/>

먼저 문서로 컬렉션을 생성해 보겠습니다. −

>db.replacingEntireDocumentDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith","StudentCountryName":"US"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cd3119bb64f4b851c3a13e8")
}

다음은 find() 메서드를 사용하여 컬렉션에서 문서를 표시하는 쿼리입니다. -

> db.replacingEntireDocumentDemo.find().pretty();

이것은 다음과 같은 출력을 생성합니다 -

{
   "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"),
   "StudentFirstName" : "John",
   "StudentLastName" : "Smith",
   "StudentCountryName" : "US"
}

다음은 전체 문서를 교체하면서 MongoDB 문서를 업데이트하는 쿼리입니다 -

>db.replacingEntireDocumentDemo.update({"StudentFirstName":"John","StudentLastName":"Smith","StudentCountryName":"US"}, {"StudentFirstName":"David","StudentLastName":"Miller","StudentCountryName":"AUS"});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

컬렉션의 모든 레코드를 다시 한 번 표시해 보겠습니다. -

> db.replacingEntireDocumentDemo.find().pretty();

이것은 다음과 같은 출력을 생성합니다 -

{
   "_id" : ObjectId("5cd3119bb64f4b851c3a13e8"),
   "StudentFirstName" : "David",
   "StudentLastName" : "Miller",
   "StudentCountryName" : "AUS"
}