AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$currentDate (업데이트 연산자)

$currentDate

The $currentDate operator sets the value of a field to the current date, either as a Date or a timestamp. The default type is Date.

다음 환경에서 호스팅되는 배포에 $currentDate 사용할 수 있습니다.

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

The $currentDate operator has the form:

{ $currentDate: { <field1>: <typeSpecification1>, ... } }

<typeSpecification> 둘 중 하나를 사용하면 됩니다.

  • 부울 true을 설정하여 필드 값을 현재 날짜로 설정하거나, 또는

  • 유형을 명시적으로 지정하는 문서 { $type: "timestamp" } 또는 { $type: "date" }입니다. 연산자는 대소문자를 구분하며 소문자 "timestamp" 또는 소문자 "date"만 사용할 수 있습니다.

내장된 문서나 배열에 <field> 기호를 지정하려면 점 표기법을사용하십시오.

MongoDB 5.0부터 업데이트 연산자는 문자열 기반 이름이 있는 문서 필드를 사전순으로 처리합니다. 숫자 이름이 있는 필드는 숫자 순서대로 처리됩니다. 자세한 내용은 업데이트 운영자 동작을 참조하십시오.

$currentDate sets the specified field to the date when $currentDate was run.

If the field does not exist, $currentDate adds the field to a document.

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $currentDate with an empty operand expression ( { } ). An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).

다음 문서를 사용하여 샘플 collection customers을 만듭니다.

db.customers.insertOne(
{ _id: 1, status: "a", lastModified: ISODate("2013-10-02T01:11:18.965Z") }
)

다음 작업은 lastModified 필드를 현재 날짜로, "cancellation.date" 필드를 현재 타임스탬프로 업데이트하고 status 필드를 "D", "cancellation.reason"를 "user request"로 업데이트합니다.

db.customers.updateOne(
{ _id: 1 },
{
$currentDate: {
lastModified: true,
"cancellation.date": { $type: "timestamp" }
},
$set: {
"cancellation.reason": "user request",
status: "D"
}
}
)

$currentDate sets the specified field to the date when $currentDate was run.

업데이트를 확인하기 위하여:

db.customers.find()

업데이트된 문서는 다음과 같습니다:

{
"_id" : 1,
"status" : "D",
"lastModified" : ISODate("2020-01-22T21:21:41.052Z"),
"cancellation" : {
"date" : Timestamp(1579728101, 1),
"reason" : "user request"
}
}

The lastModified field is set to the date when $currentDate was run in the update example shown earlier.

업데이트 메서드는 집계 파이프라인을 사용할 수 있습니다. 구체적으로 설명하면, 이전 예시는 집계 단계 $set 와 집계 변수 NOW(현재 날짜/시간의 경우), CLUSTER_TIME(현재 타임스탬프의 경우)를 사용해 다음과 같이 다시 작성할 수 있습니다.

팁

  • 집계 변수에 액세스하려면 변수 앞에 이중 달러 기호 $$를 붙이고 따옴표로 묶으십시오.

  • CLUSTER_TIME 는 복제본 세트와 샤딩된 클러스터에서만 사용할 수 있습니다.

  • NOW 및 CLUSTER_TIME 값은 파이프라인 전체에서 동일하게 유지됩니다.

db.customers.updateOne(
{ _id: 1 },
[
{ $set: { lastModified: "$$NOW", cancellation: {date: "$$CLUSTER_TIME", reason: "user request"}, status: "D" } }
]
)

작업 후 collection을 쿼리하여 업데이트를 확인할 수 있습니다.

db.customers.find().pretty()

쿼리는 다음 문서를 반환해야 합니다.

{
"_id" : 1,
"status" : "D",
"lastModified" : ISODate("2020-01-22T21:02:18.994Z"),
"cancellation" : {
"date" : Timestamp(1579726934, 2),
"reason" : "user request"
}
}
이 페이지 평가하기

이 페이지의 내용