ホームページ  >  記事  >  ウェブフロントエンド  >  NodeJs を使用して MongoDB 上に単純なプロジェクトを作成することから始める

NodeJs を使用して MongoDB 上に単純なプロジェクトを作成することから始める

王林
王林オリジナル
2024-07-19 04:24:52272ブラウズ

Start With Creating a Simple Project on MongoDB with NodeJs

MongoDB への接続

const { MongoClient } = require('mongodb');

const uri = "your_mongodb_uri";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

async function run() {
  try {
    await client.connect();
    console.log("Connected to MongoDB");
  } finally {
    await client.close();
  }
}

run().catch(console.dir);

単一のドキュメントの挿入

const insertDocument = async (client, newDoc) => {
  const result = await client.db("database_name").collection("collection_name").insertOne(newDoc);
  console.log(`New document created with the following id: ${result.insertedId}`);
};

// Usage
insertDocument(client, { name: "John Doe", age: 30 });

複数のドキュメントの挿入

const insertDocuments = async (client, newDocs) => {
  const result = await client.db("database_name").collection("collection_name").insertMany(newDocs);
  console.log(`${result.insertedCount} documents were inserted`);
};

// Usage
insertDocuments(client, [
  { name: "John Doe", age: 30 },
  { name: "Jane Doe", age: 25 }
]);

単一のドキュメントの検索

const findOneDocument = async (client, query) => {
  const result = await client.db("database_name").collection("collection_name").findOne(query);
  console.log(result);
};

// Usage
findOneDocument(client, { name: "John Doe" });

複数のドキュメントの検索

const findDocuments = async (client, query) => {
  const cursor = client.db("database_name").collection("collection_name").find(query);
  const results = await cursor.toArray();
  console.log(results);
};

// Usage
findDocuments(client, { age: { $gt: 20 } });

単一のドキュメントの更新

const updateDocument = async (client, filter, updateDoc) => {
  const result = await client.db("database_name").collection("collection_name").updateOne(filter, { $set: updateDoc });
  console.log(`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`);
};

// Usage
updateDocument(client, { name: "John Doe" }, { age: 35 });

複数のドキュメントの更新

const updateDocuments = async (client, filter, updateDoc) => {
  const result = await client.db("database_name").collection("collection_name").updateMany(filter, { $set: updateDoc });
  console.log(`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`);
};

// Usage
updateDocuments(client, { age: { $lt: 30 } }, { isActive: true });

単一ドキュメントの削除

const deleteDocument = async (client, query) => {
  const result = await client.db("database_name").collection("collection_name").deleteOne(query);
  console.log(`${result.deletedCount} document(s) was/were deleted`);
};

// Usage
deleteDocument(client, { name: "John Doe" });

複数のドキュメントを削除する

const deleteDocuments = async (client, query) => {
  const result = await client.db("database_name").collection("collection_name").deleteMany(query);
  console.log(`${result.deletedCount} document(s) was/were deleted`);
};

// Usage
deleteDocuments(client, { age: { $lt: 25 } });

インデックスの作成

const createIndex = async (client, index) => {
  const result = await client.db("database_name").collection("collection_name").createIndex(index);
  console.log(`Index created: ${result}`);
};

// Usage
createIndex(client, { name: 1 });

続きを読む

JS の矢印演算子

AWS Lambda によるサーバーレス コンピューティングの威力

以上がNodeJs を使用して MongoDB 上に単純なプロジェクトを作成することから始めるの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。