0

I have a Company Schema that will hold some data for that company and an array of posts. When a user submits a post I use passport to decode the token and get some user information. Inside that user information there is an object ID which allows me to find the company that the user belongs to.

So now I have found the company that the user belongs to I need to save the submitted post into the board_posts array inside this company

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const BoardPostSchema = new Schema({
    name: {
        type: String
    }
});

const CompanySchema = new Schema({
    company_name: {
        type: String
    },
    board_posts: [BoardPostSchema],
});

module.exports = Company = mongoose.model('companies', CompanySchema);
router.post('/new-opportunity',  passport.authenticate('jwt', { 
    session: false 
}), (req, res) => {
    let user = req.user;
    let newPost = req.body;
    let companyId = user.company_id;

    const boardPost = {
        name: newPost.name
    };

    Company.find({'_id': companyId})
        .then(company => {
            // push boardPost into this company.board_posts array
        })
        .catch(error => {

        });
});

3 Answers 3

1

An alternative solution with findByIdAndUpdate:

router.post("/new-opportunity", passport.authenticate("jwt", { session: false }), (req, res) => {
  let user = req.user;
  let newPost = req.body;
  let companyId = user.company_id;

  const boardPost = {
    name: newPost.name,
  };

  Company.findByIdAndUpdate(
    companyId,
    {
      $push: {
        board_posts: boardPost,
      },
    },
    {
      new: true,
    }
  )
    .then((company) => {
      console.log("Updated compay if found:", company);
      res.send(company);
    })
    .catch((error) => {
      console.log(error);
      res.status(500);
    });
});

Or if you want only update status, you can use updateOne:

router.post("/new-opportunity", passport.authenticate("jwt", { session: false }), (req, res) => {
  let user = req.user;
  let newPost = req.body;
  let companyId = user.company_id;

  const boardPost = {
    name: newPost.name,
  };

  Company.updateOne(
    { _id: companyId },
    {
      $push: {
        board_posts: boardPost,
      },
    }
  )
    .then((result) => {
      console.log(result);
      // result.n; // Number of documents matched
      // result.nModified; // Number of documents modified
      res.send(result);
    })
    .catch((error) => {
      console.log(error);
      res.status(500);
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

What does "new: true" do, does that just create a new object? If I set it to false would I be able to use a similar method to update a post inside the board_posts array?
with {new: true} option we get the updated object. It is useful to see the updated object so that we can easily understand whether the push worked.
So if I don't want to return the any data and instead just return a status then I don't need this part?
@Reece if you aren't interested in the updated object, better to use updateOne method, I added is to the answer.
1

You can use $push and update

router.post('/new-opportunity',  passport.authenticate('jwt', { 
    session: false 
}), (req, res) => {
    let user = req.user;
    let newPost = req.body;
    let companyId = user.company_id;

    const boardPost = {
        name: newPost.name
    };

    Company.update({_id: user.company_id},{
         $push{
             //the things you want to add
         }
    });
});

Hopefully this is what you want to do!

Comments

0

Yes you can use the $pushand findOneAndUpdate operator. It would look nicer if you use the async/await approach.

router.post('/new-opportunity',  passport.authenticate('jwt', { 
   session: false 
}), async (req, res) => {
   let user = req.user;
   let newPost = req.body;
   let companyId = user.company_id;

   const boardPost = {
       name: newPost.name
   };

  let response = await Company.findOneAndUpdate({'_id': companyId}, {
     $push: {
        board_posts: "testword1"
     }
  },{ new: true })  //i set this to true so mongodb will return me the new updated document

  res.send(response);

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.