LoginSignup
0
1

Deno: MongoDB のデータを更新 (Update)

Last updated at Posted at 2020-05-28

Deno で MongoDB のデータを更新します。

mongo_update.ts
//
//	mongo_update.ts
//
//					Jan/06/2024
// ----------------------------------------------------------------
function get_current_date_proc ()
{
	const today = new Date ();
	var ddx = today.getFullYear () + "-" + (today.getMonth () +1)
	ddx += "-" + today.getDate ()

	return ddx
}

// ----------------------------------------------------------------
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 key:string =Deno.args[0]
const population_in:number =parseInt(Deno.args[1])

console.log (key + "\t" + population_in)

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 date_mod = get_current_date_proc ()

const { matchedCount, modifiedCount, upsertedId } = await cities.updateOne(
  { key: key },
  { $set: { population: population_in, date_mod: date_mod } },
)

console.log ("*** 終了 ***")

// ----------------------------------------------------------------
.env
user='scott'
password='tiger123'
db='city'
collection='saitama'

実行コマンド

deno run --allow-net --allow-write --allow-read --unstable  mongo_update.ts t1164 78253900
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1