特定の月|年(日付ではない)を取得するためのMongoDBクエリ?
$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
} -
ネストされた値を取得するためのMongoDBクエリを作成しますか?
ドット表記を使用して、ネストされた値を取得できます。まず、ドキュメントを使用してコレクションを作成しましょう > db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "John", ... "EmployeeDetails" : ... { ... ...
-
MongoDBでDate/ISODateのコンポーネントを取得しますか?
MongoDBでDate/ISODateのコンポーネントを取得するには、コレクションに日付を含むドキュメントを作成しましょう。それでは、MongoDBのDate/ISODateのコンポーネントを取得しましょう > db.componentOfDateDemo.insert({"ShippingDate":new Date()}); WriteResult({ "nInserted" : 1 }) 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです > db.componentOfDateDe