翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$isoWeek
Amazon DocumentDB の$isoWeek演算子は、日付の ISO 週番号を返します。ISO 週の日付システムは、1 年の週を番号付けする方法であり、新しい年の最初の週は、その年の最初の木曜日を含む週です。これは、新年の最初の週が 1 月 1 日を含む週であるグレゴリオ暦とは異なります。
パラメータ
なし
例 (MongoDB シェル)
次の例は、 $isoWeek演算子を使用して特定の日付の ISO 週番号を取得する方法を示しています。
サンプルドキュメントを作成する
db.dates.insertMany([
{ _id: 1, date: new ISODate("2022-01-01") },
{ _id: 2, date: new ISODate("2022-12-31") },
{ _id: 3, date: new ISODate("2023-01-01") }
])
クエリの例
db.dates.aggregate([
{
$project: {
_id: 1,
isoWeek: { $isoWeek: "$date" }
}
}
])
出力
[
{ "_id": 1, "isoWeek": 52 },
{ "_id": 2, "isoWeek": 52 },
{ "_id": 3, "isoWeek": 1 }
]
コードの例
$isoWeek コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
const db = client.db('test');
const collection = db.collection('dates');
const result = await collection.aggregate([
{
$project: {
_id: 1,
isoWeek: { $isoWeek: "$date" }
}
}
]).toArray();
console.log(result);
client.close();
}
example();
- Python
-
from pymongo import MongoClient
def example():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
db = client['test']
collection = db['dates']
result = list(collection.aggregate([
{
'$project': {
'_id': 1,
'isoWeek': { '$isoWeek': '$date' }
}
}
]))
print(result)
client.close()
example()