首頁 > web前端 > js教程 > 從使用 NodeJs 在 MongoDB 上建立一個簡單專案開始

從使用 NodeJs 在 MongoDB 上建立一個簡單專案開始

王林
發布: 2024-07-19 04:24:52
原創
416 人瀏覽過

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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板