Assuming, var friend = { firstName: 'Harry', lastName: 'Potter' };
There are two options you have:
Update the model in-memory, and save (plain javascript array.push):
person.friends.push(friend); person.save(done);
or
PersonModel.update( { _id: person._id }, { $push: { friends: friend } }, done );
I always try and go for the first option when possible, because it’ll respect more of the benefits that mongoose gives you (hooks, validation, etc.).
However, if you are doing lots of concurrent writes, you will hit race conditions where you’ll end up with nasty version errors to stop you from replacing the entire model each time and losing the previous friend you added. So only go to the former when it’s absolutely necessary.
Related Posts:
- nodemon – app crashed – waiting for file changes before starting
- How to update a array value in Mongoose
- mongoError: Topology was destroyed
- “Topology was destroyed” when using MongoDB with native driver and Express.js
- Error: getaddrinfo ENOTFOUND in nodejs for get call
- Mongoose: findOneAndUpdate doesn’t return updated document
- How to Create and Use Enum in Mongoose
- (node:63208) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead
- Difference between MongoDB and Mongoose
- E11000 duplicate key error index in mongodb mongoose
- Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”
- passport.js passport.initialize() middleware not in use
- In Mongoose, how do I sort by date? (node.js)
- Properly close mongoose’s connection once you’re done
- Mongodb: failed to connect to server on first connect
- How to drop a database with Mongoose?
- Cannot access mongodb through browser – It looks like you are trying to access MongoDB over HTTP on the native driver port
- Node.exe Stuck at 100% CPU
- How do I debug error ECONNRESET in Node.js?
- How do I debug error ECONNRESET in Node.js?
- What is Express.js?
- What does body-parser do with express?
- node.js, Error: Cannot find module ‘express’
- node.js, Error: Cannot find module ‘express’
- nodemon not working: -bash: nodemon: command not found
- Failed to load resource: net::ERR_CONNECTION_REFUSED : Nodejs
- SyntaxError: Unexpected token import – Express
- Error: No default engine was specified and no extension was provided
- Significance of port 3000 in Express apps
- ExpressJS – throw er Unhandled error event
- NodeJS / Express: what is “app.use”?
- Why am I getting “Error socket hang up” response?
- First Heroku deploy failed `error code=H10`
- How to get GET (query string) variables in Express.js on Node.js?
- What is Procfile? and Web and Worker
- What is Procfile? and Web and Worker
- Error: Cannot find module ‘ejs’
- First Heroku deploy failed `error code=H10`
- In express how do I redirect a user to an external url?
- ExpressJS – throw er Unhandled error event
- ENOENT, no such file or directory
- ExpressJS : res.redirect() not working as expected?
- Expressjs / Node.js – res.redirect() not loading page
- MissingSchemaError: Schema hasn’t been registered for model “User”
- NodeJS w/Express Error: Cannot GET /
- Express.js req.body undefined
- Unable to connect to mongolab, Getting MongoError: auth failed
- Enabling HTTPS on express.js
- How do I remove documents using Node.js Mongoose?
- heroku is giving me Method not allowed
- ExpressJS vs MeteorJS
- db.collection is not a function when using MongoClient v3.0
- Cannot POST / error using express
- Route.get() requires callback functions but got a “object Undefined”
- nodemon not working properly
- npm WARN package.json: No repository field
- Advice on loopback.js vs express js
- How do I use HTML as the view engine in Express?
- Hapi/Joi Validation: How to process text to a specific format
- How to sort a collection by date in MongoDB?
- How to sort in mongoose?
- bodyParser is deprecated express 4
- MongoDB data/db not found
- How to install a previous exact version of a NPM package?
- How to fix ‘Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.’
- Node.js version on the command line? (not the REPL)
- bash: npm: command not found?
- node.js string.replace doesn’t work?
- What is “export default” in JavaScript?
- What is NODE_ENV and how to use it in Express?
- What is the difference between __dirname and ./ in node.js?
- How can the default node version be set using NVM?
- What does “res.render” do, and what does the html file look like?
- Angular – ng: command not found
- How to downgrade Node version
- stop all instances of node.js server
- What Are “npm run dev” and “npm run prod”
- Find the version of an installed npm package
- Error: EACCES: permission denied, access ‘/usr/local/lib/node_modules’
- Error “Cannot find module ‘npmlog'” after “npm update -g”
- error: This is probably not a problem with npm. There is likely additional logging output above
- Express error – TypeError: Router.use() requires middleware function but got a Object
- npm – “Can’t find Python executable “python”, you can set the PYTHON env variable.”
- Node.js: Difference between req.query[] and req.params
- Node.js + Nginx – What now?
- Make Axios send cookies in its requests automatically
- What could cause “connect ETIMEDOUT” error when the URL is working in browser?
- “invalid ELF header” when using the nodejs “ref” module on AWS Lambda
- Entrypoint undefined = index.html using HtmlWebpackPlugin
- How can I use webpack with express?
- NPM self_signed_cert_in_chain
- nodejs – first argument must be a string or Buffer – when using response.write with http.request
- How can I use an ES6 import in Node.js? [duplicate]
- npm install with error: `gyp` failed with exit code: 1
- How to stop Node.js server?
- unexpected strict mode reserved word — yield? Node v0.11, harmony, es6
- Nodejs – Redirect url
- ode.js vs. meteor.js what’s the difference?
- Can pm2 run an ‘npm start’ script
- nvm is not compatible with the npm config “prefix” option: