Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.4 kB
3
Indexable
Never
const mongoose = require("mongoose");

async function performTaskOperations() {
  try {
    await mongoose.connect("mongodb://localhost:27017/test_db", { useNewUrlParser: true, useUnifiedTopology: true });

    const Schema = mongoose.Schema;
    const TaskSchema = new Schema({
      project: String,
      description: String
    });

    const Task = mongoose.model("Task", TaskSchema);

    const task = new Task({ project: "Malowanie", description: "Pomalować rower na czerwono" });

    await task.save();
    console.log("Zadanie zostało dodane");

    const foundTask = await Task.findOne({ 'project': 'Malowanie' });
    if (foundTask) {
      console.log(foundTask._id, foundTask.description);
    } else {
      console.log("Task not found");
    }

		await Task.updateOne({"_id": '657469701b6105d337f9b49a'}, {description: "Pomalować rower na zielono"}, {multi: false});
		const updatedTask = await Task.findOne({ '_id': '657469701b6105d337f9b49a' });

		const task2 = new Task({ project: "Malowanie", description: "Do usuniecia" });

    await task2.save();

		const resdel = await Task.findByIdAndDelete(task2.id);
		console.log(resdel);

    mongoose.disconnect(); // Disconnect after all operations complete
  } catch (err) {
    console.error("Error occurred:", err);
    mongoose.disconnect(); // Disconnect on error
  }
}

performTaskOperations();
Leave a Comment