Search code examples
node.jsmongodbmongoose

Cannot Update Two Mongodb Collections in one Mutation


I'm working on a project using node.js with graphql and mongodb as my database in the backend. In this project, I have a Project collection and an EditedProject collection, which contains the edits to a project that a user made. Additionally, I have an editProject mutation, which changes the Project information to the approved EditedProject information based off of the originalProjectId. In this mutation, I need the document in the Project collection to be updated and the document in the EditedProject collection containing these edits to be deleted. The problem that I'm having is that only the Project collection is being updated. However, if I comment out

const project = await Project.updateOne({"_id":new ObjectId(originalProjectId)}, 
            {$set: { 
            "date": input.date,
            "type": input.type, 
            "edited":'Approved', 
           }})
const editResult = await project.save();
const deleteEditedProject = await EditedProject.find({"originalProjectId":originalProjectId})
const deleteResult = await deleteEditedProject.save();  

goes through. Following const project = await Project.updateOne({ ... const editResult = await project.save();, console.log(originalProjectId) does not return anything in the terminal. Is this due to .save()? Is there a way to work around this? I can't seem to figure out how to get this to work, and I would really appreciate any help or advice. Thank you!

import EditedProject from '../../models/EditedProject.js';
import Project from '../../models/Project.js';
import { requireAuth } from '../../services/auth.js';
import { ObjectId } from "mongodb";


export default { 
editeProject: async (_, {input}) => {
        try {
            const originalProjectId = input.originalProjectId
            const project = await Project.updateOne({"_id":new ObjectId(originalProjectId)}, 
            {$set: { 
            "date": input.date,
            "type": input.type, 
            "edited":'Approved', 
           }})
           const editResult = await project.save();
           console.log(originalProjectId)
           const deleteEditedProject = await EditedProject.find({"originalProjectId":originalProjectId})
           const deleteResult = await deleteEditedProject.save();        
           return originalProjectId;
            } 
         catch (error) {
            return {
            errorMessage:error.message
            };
            }
        },
}

Solution

  • There is no need to use save after updateOne if you want updated record than use findOneAndUpdate() for Example:

    db.collection.findOneAndUpdate(filter, update,{new:1})
    

    above query will give you updated record

    And If you want to perform update on two collection then you can use the transaction.

    In mongoose you can achieve it by using transaction refer the official documentation https://mongoosejs.com/docs/transactions.html