Home > Web Front-end > JS Tutorial > Start With Creating a Simple Project on MongoDB with NodeJs

Start With Creating a Simple Project on MongoDB with NodeJs

王林
Release: 2024-07-19 04:24:52
Original
424 people have browsed it

Start With Creating a Simple Project on MongoDB with NodeJs

Connecting to 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);
Copy after login

Inserting a Single Document

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 });

Copy after login

Inserting Multiple Documents

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 }
]);

Copy after login

Finding a Single Document

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" });

Copy after login

Finding Multiple Documents

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 } });

Copy after login

Updating a Single Document

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 });

Copy after login

Updating Multiple Documents

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 });

Copy after login

Deleting a Single Document

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" });

Copy after login

Deleting Multiple Documents

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 } });

Copy after login

Creating an Index

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 });

Copy after login

Read More

Arrow Operator in JS

Power of Serverless Computing with AWS Lambda

The above is the detailed content of Start With Creating a Simple Project on MongoDB with NodeJs. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template