Deno で MongoDB のデータを読みます。
mongo_read.ts
//
// mongo_read.ts
//
// Jan/06/2024
// ----------------------------------------------------------------
import { MongoClient } from "https://deno.land/x/mongo@v0.32.0/mod.ts";
import { config } from "https://deno.land/x/dotenv/mod.ts"
console.log ("*** 開始 ***")
const config_env:any = config()
const user:string = config_env.user
const password:string = config_env.password
const db_name:string = config_env.db
const collection_name:string = config_env.collection
const host:string = user + ':' + password + '@localhost'
const port:number = 27017
const url:string = 'mongodb://' + host + ':' + port
console.log("Connecting to " + host + ":" + port)
const client = new MongoClient()
await client.connect(url)
const db = client.database(db_name)
const cities = db.collection(collection_name)
const count = await cities.countDocuments({})
console.log(count)
const cursor = cities.find({ key: { $ne: null } })
for await (const item of cursor) {
var str_out = item.key + "\t" + item.name
str_out += "\t" + item.population
str_out += "\t" + item.date_mod
console.log(str_out)
}
console.log ("*** 終了 ***")
// ----------------------------------------------------------------
.env
user='scott'
password='tiger123'
db='city'
collection='saitama'
実行コマンド
deno run --allow-net --allow-write --allow-read --unstable mongo_read.ts