0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

プログラム

データの挿入

insert_authors.js
// ----------------------------------------------------------------
//	insert_authors.js
//
//						Jun/27/2024
// ----------------------------------------------------------------
const mongoose = require('mongoose');

// MongoDB コネクションの設定
const mongoURI = 'mongodb://localhost:27017/my_database';

// MongoDB への接続
mongoose.connect(mongoURI);

// 接続が確立された時のコールバック
const db = mongoose.connection;
db.on('connected', function() {
  console.log('MongoDBに接続しました');
});

// データのスキーマ定義
const authorSchema = new mongoose.Schema({
  name: String,
  birth: Number
});

// モデルの作成
const Author = mongoose.model('Author', authorSchema, 'my_collection');

// データの挿入
const author1 = new Author({ name: '森鴎外', birth: 1862 });
const author2 = new Author({ name: '夏目漱石', birth: 1867 });
const author3 = new Author({ name: '島崎藤村', birth: 1872 });

// データの保存
const saveAuthors = async () => {
  try {
    await author1.save();
    await author2.save();
    await author3.save();
    console.log('データを保存しました');
  } catch (error) {
    console.error('データの保存に失敗しました', error);
  } finally {
    // 接続の切断
    mongoose.connection.close();
  }
};

// ----------------------------------------------------------------
saveAuthors();

// ----------------------------------------------------------------

データの取得

read_authors.js
// ----------------------------------------------------------------
//	read_authors.js
//
//						Jun/27/2024
// ----------------------------------------------------------------
const mongoose = require('mongoose');

// MongoDB コネクションの設定
const mongoURI = 'mongodb://localhost:27017/my_database';

// MongoDB への接続
mongoose.connect(mongoURI);

// 接続が確立された時のコールバック
const db = mongoose.connection;
db.on('connected', function() {
  console.log('MongoDBに接続しました');
});

// データのスキーマ定義
const authorSchema = new mongoose.Schema({
  name: String,
  birth: Number
});

// モデルの作成
const Author = mongoose.model('Author', authorSchema, 'my_collection');


// データの取得
const getAuthors = async () => {
  try {
    const authors = await Author.find();
    console.log(authors);
  } catch (error) {
    console.error('データの取得に失敗しました', error);
  } finally {
    // 接続の切断
    mongoose.connection.close();
  }
};
// ----------------------------------------------------------------

getAuthors();
// ----------------------------------------------------------------

実行コマンド

データの挿入

export NODE_PATH=/usr/local/lib/node_modules
node insert_authors.js

データの取得

export NODE_PATH=/usr/local/lib/node_modules
node read_authors.js
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?