SvenWesterlaken / mongo4j

A mongoose plugin to automatically maintain nodes & relationships in neo4j
https://www.npmjs.com/package/mongo4j
MIT License
14 stars 4 forks source link

When I deleted one object from mongodb, why the node in neo4j still there? #63

Closed Jasper-Joe closed 4 years ago

Jasper-Joe commented 4 years ago

How do I fix it? Thanks!

SvenWesterlaken commented 4 years ago

As with your other issue, could you show some code where you're trying to delete the mongodb object?

Jasper-Joe commented 4 years ago
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const mongo4j = require("mongo4j");

const reservationSchema = mongoose.Schema({
  _id: { type: mongoose.Schema.Types.ObjectId, neo_prop: true },
  employee: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Employee",
    required: true,
    neo_prop: true,
    neo_rel_name: "Made by",
  },
});

reservationSchema.plugin(mongo4j.plugin());
module.exports = mongoose.model("Reservation", reservationSchema);
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const mongo4j = require("mongo4j");
const employeeSchema = mongoose.Schema({
  _id: { type: mongoose.Schema.Types.ObjectId, neo_prop: true },
  name: { type: String, required: true, neo_prop: true },
  employee_id: { type: String, required: true, neo_prop: true },
  currentSeat: { type: String },
});
employeeSchema.plugin(mongo4j.plugin());
module.exports = mongoose.model("Employee", employeeSchema);

I send a delete request like on postman: http://localhost:5000/employees/5f02449bcb1902160f60dbd8

The employee that I deleted was still there in neo4j database as a node. I am not sure where went wrong. Thanks for your reply!

SvenWesterlaken commented 4 years ago

How do you delete the employee after you sent the request, guess you have an endpoint that handles that request? Could you show me the code of that part?

PS. you don't need to add neo_prop: true to _id, this will be added automatically if you add the plugin to the schema. (this will be saved as m_id in neo4j).

Jasper-Joe commented 4 years ago

Hi, I really appreciate your reply. It is very helpful. Here is the router for deletion of employees. Seems like the data is deleted in mongodb, but not in neo4j. Any suggestions on that? Thanks!

router.delete("/:employee_id", (req, res, next) => {
  const id = req.params.employee_id;
  Employee.remove({ _id: id })
    .exec()
    .then((result) => {
      res.status(200).json({
        message: "Employee account deleted",
        request: {
          type: "POST",
          url: "http://localhost:3000/employees",
          body: { name: "String", employee_id: "String" },
        },
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({
        error: err,
      });
    });
});

module.exports = router;
SvenWesterlaken commented 4 years ago

As I thought, you're deleting it by using the Model. This won't trigger the hook that deletes the node in neo4j as described in the documenation of mongoose:

Like Model.remove(), this function does not trigger pre('remove') or post('remove') hooks.

I will mark this as an enhancement and may look into implementing the deletion in neo4j for the Schema.remove() function like you used it. For now this change should fix your problem:

router.delete("/:employee_id", (req, res, next) => {
  const id = req.params.employee_id;
  Employee.findById(id)
    .exec()
    .then((employee) => employee.remove())
    .then((result) => {
      res.status(200).json({
        message: "Employee account deleted",
        request: {
          type: "POST",
          url: "http://localhost:3000/employees",
          body: { name: "String", employee_id: "String" },
        },
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({
        error: err,
      });
    });
});

module.exports = router;

I haven't tested this code so let me know if you still have any issues. But what it basically does, is finding the model in the database first, which should return a prototype/instance of a Model/Document on which you can call remove(). This will trigger the hooks I created for removing it in neo4j as well.

Jasper-Joe commented 4 years ago

Thanks a lot! Now it is working perfectly. You really saved my life. Thank you for your great project, it really helps me a lot. Keep it up!