$second - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$second

Amazon DocumentDB の $second演算子は、日付またはタイムスタンプから秒コンポーネントを抽出します。これは、日付またはタイムスタンプフィールドから秒値を取得するために使用されます。

パラメータ

  • expression: 秒値を抽出する日付またはタイムスタンプフィールド。この式は、フィールドパスでも、日付またはタイムスタンプに解決される有効な式でもかまいません。

例 (MongoDB シェル)

次の例は、 $second演算子を使用して日付フィールドから秒コンポーネントを抽出する方法を示しています。

サンプルドキュメントを作成する

db.users.insertMany([ { name: "John", dob: new Date("1990-05-15T12:30:45Z") }, { name: "Jane", dob: new Date("1985-09-20T23:59:59Z") }, { name: "Bob", dob: new Date("2000-01-01T00:00:00Z") } ]);

クエリの例

db.users.aggregate([{ $project: { name: 1, dobSeconds: { $second: "$dob" } } }])

出力

[ { "_id" : ObjectId("6089a9c306a829d1f8b456a1"), "name" : "John", "dobSeconds" : 45 }, { "_id" : ObjectId("6089a9c306a829d1f8b456a2"), "name" : "Jane", "dobSeconds" : 59 }, { "_id" : ObjectId("6089a9c306a829d1f8b456a3"), "name" : "Bob", "dobSeconds" : 0 } ]

コードの例

$second コマンドを使用するためのコード例を表示するには、使用する言語のタブを選択します。

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 users = db.collection('users'); const result = await users.aggregate([{ $project: { name: 1, dobSeconds: { $second: '$dob' } } }]).toArray(); console.log(result); await 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'] users = db['users'] result = list(users.aggregate([{'$project': {'name': 1, 'dobSeconds': {'$second': '$dob'}}}])) print(result) client.close() example ()