Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
How to insert an Array in Node js and MongoDB

Well, I am stuck since a long time on how to treat properly an array in Node js in order to persist it in my mongobd database. I tried a lot of stuff, i am feeling close to success, but still stuck with it with the var filmDataSchema and the var film syntax.

I really need a little help, please:

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Are you using mongoose? it's an elegant mongodb object modeling for node.js and has a really nice interface to handle arrays so let's say you have a student model with grades array and you want to add a new grade, you can use the '$push' (if) or $addToSet operators like so

StudentModel.update(
   { _id: student._id }, 
   { $addToSet: { grades: grade } }
);

StudentModel.update(
   { _id: student._id }, 
   { $push: { grades: grade } }
);

use $addToSet if you want only unique items to be pushed into array. use $push if you just want to add the object to array whether or not the object is already there

more about mongoose can be found here https://mongoosejs.com/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!