首页 >web前端 >js教程 >从使用 NodeJs 在 MongoDB 上创建一个简单项目开始

从使用 NodeJs 在 MongoDB 上创建一个简单项目开始

王林
王林原创
2024-07-19 04:24:52395浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn