$minute - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$minute

Tahap pipeline $minute agregasi di Amazon DocumentDB mengekstrak nilai menit dari bidang tanggal atau stempel waktu.

Operator ini berguna saat Anda perlu melakukan perhitungan atau pengelompokan berbasis tanggal dan waktu dalam jalur agregasi Anda.

Parameter

  • expression: Bidang tanggal atau stempel waktu untuk mengekstrak nilai menit.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan bagaimana menggunakan $minute operator untuk mengelompokkan dokumen dengan nilai menit yang diekstrak dari bidang stempel waktu dan menghitung jumlah dokumen di setiap grup.

Buat dokumen sampel

db.events.insertMany([ { timestamp: new Date("2023-04-15T10:30:25.000Z") }, { timestamp: new Date("2023-04-15T10:30:35.000Z") }, { timestamp: new Date("2023-04-15T10:31:05.000Z") }, { timestamp: new Date("2023-04-15T10:31:45.000Z") }, { timestamp: new Date("2023-04-15T10:32:15.000Z") } ]);

Contoh kueri

db.events.aggregate([ { $group: { _id: { minute: { $minute: "$timestamp" } }, count: { $count: {} } } }, { $sort: { "_id.minute": 1 } } ]);

Keluaran

[ { "_id": { "minute": 30 }, "count": 2 }, { "_id": { "minute": 31 }, "count": 2 }, { "_id": { "minute": 32 }, "count": 1 } ]

Contoh kode

Untuk melihat contoh kode untuk menggunakan $minute perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

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('events'); await collection.insertMany([ { timestamp: new Date("2023-04-15T10:30:25.000Z") }, { timestamp: new Date("2023-04-15T10:30:35.000Z") }, { timestamp: new Date("2023-04-15T10:31:05.000Z") }, { timestamp: new Date("2023-04-15T10:31:45.000Z") }, { timestamp: new Date("2023-04-15T10:32:15.000Z") } ]); const result = await collection.aggregate([ { $group: { _id: { minute: { $minute: "$timestamp" } }, count: { $count: {} } } }, { $sort: { "_id.minute": 1 } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient from datetime import datetime 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['events'] collection.insert_many([ {'timestamp': datetime(2023, 4, 15, 10, 30, 25)}, {'timestamp': datetime(2023, 4, 15, 10, 30, 35)}, {'timestamp': datetime(2023, 4, 15, 10, 31, 5)}, {'timestamp': datetime(2023, 4, 15, 10, 31, 45)}, {'timestamp': datetime(2023, 4, 15, 10, 32, 15)} ]) result = list(collection.aggregate([ { '$group': { '_id': { 'minute': {'$minute': '$timestamp'} }, 'count': {'$count': {}} } }, {'$sort': {'_id.minute': 1}} ])) print(result) client.close() example()