MongoDBメモ

仕事でmongoDBを使うことになりました。
その時のメモです。

419 views

<?php
ini_set('display_errors', "On");
require 'vendor/autoload.php'; // include Composer's autoloader
$client = new MongoDB\Client("mongodb://192.168.1.87:27017");
$collection = $client->BlasMQ->sensor;

print("レコード数を求める\n");

//レコード数を求める。
$count = $collection->countDocuments();
print("{$count}\n");

print("キーワードで検索してみる\n");
//キーワードで検索してみる。
$result = $collection->find(["record.serial_number"=>"376585033"])->toArray();
print_r($result);


//ソートしてみる。
print("時間でソートする\n");
$filter = [];
$options = ['sort' => ["create_at" => -1]];
$result = $collection->find($filter, $options)->toArray();
foreach($result as $entry) {
    print("{$entry['create_at']}\n");
}

//ソートして20件だけ取得する。
print("時間でソートして20件だけ取得する\n");
$filter = [];
$options = [
    'sort' => ["create_at" => -1],
    'limit' => 5
    ];

$result = $collection->find($filter, $options)->toArray();
foreach($result as $entry) {
    print("{$entry['create_at']}\n");
}

Page 4 of 5.

前のページ 次のページ



[添付ファイル]


お問い合わせ

プロフィール

すぺぺぺ

自己紹介

本サイトの作成者。
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
ChatGPTで自動プログラム作成に取り組み中。

サイト/ブログ

https://www.osumoi-stdio.com/novel/

ツイッター

@darkimpact0626