$month 투영 연산자와 함께 집계 프레임워크를 사용할 수 있습니다. 먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.specificMonthDemo.insertOne({"StudentName":"Larry","StudentDateOfBirth":new ISODate('1995-01-12')});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb9a9ca8f1d1b97daf71819")
}
> db.specificMonthDemo.insertOne({"StudentName":"Chris","StudentDateOfBirth":new ISODate('1999-12-31')});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb9a9db8f1d1b97daf7181a")
}
> db.specificMonthDemo.insertOne({"StudentName":"David","StudentDateOfBirth":new ISODate('2000-06-01')});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cb9a9ee8f1d1b97daf7181b")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.specificMonthDemo.find().pretty();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cb9a9ca8f1d1b97daf71819"),
"StudentName" : "Larry",
"StudentDateOfBirth" : ISODate("1995-01-12T00:00:00Z")
}
{
"_id" : ObjectId("5cb9a9db8f1d1b97daf7181a"),
"StudentName" : "Chris",
"StudentDateOfBirth" : ISODate("1999-12-31T00:00:00Z")
}
{
"_id" : ObjectId("5cb9a9ee8f1d1b97daf7181b"),
"StudentName" : "David",
"StudentDateOfBirth" : ISODate("2000-06-01T00:00:00Z")
} 다음은 날짜가 아닌 특정 월|연도를 가져오는 쿼리입니다. -
> db.specificMonthDemo.aggregate([ {$project: {StudentName: 1, StudentDateOfBirth:
{$month: '$StudentDateOfBirth'}}}, {$match: {StudentDateOfBirth: 01}} ]).pretty(); 이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cb9a9ca8f1d1b97daf71819"),
"StudentName" : "Larry",
"StudentDateOfBirth" : 1
}