showing results for - "mongoose update subdocument by id"
Elisa
11 Aug 2018
1//this method for add data to subdocument
2BlogPost.findById(req.params.postId, function (err, post) {
3    var subDoc = post.comments.id(req.params.commentId);
4    subDoc = req.body;
5    post.save(function (err) {
6        if (err) return res.status(500).send(err);
7        res.send(post);
8    });
9});
10
11// alternative second method you can use this
12BlogPost.findOneAndUpdate({_id: req.params.postId}, {$push:{ subDoc: req.body }}, (err, doc) => {
13  	// do something here
14});
15
Vincenzo
18 Sep 2016
1const mongoose = require('mongoose');
2const bcrypt = require('bcryptjs');
3const saltRounds = 10;
4const Role = require('../config/roles')
5const subscription = require('./subscriptionModel')
6
7const Schema = mongoose.Schema;
8
9const userModel = new Schema({
10    created: { type: Date, default: Date.now },
11    updated: { type: Date, default: Date.now},
12    fullname: {
13        type: String,
14        required: true
15    },
16    email: {
17        type: String,
18        required: true,
19        unique: true
20    },
21    birthDate: {
22        type: String,
23        required: true
24    },
25    password: {
26        type: String,
27        required: true
28    },
29    profileImage: {
30        type: String
31    },
32    role: {
33        type: String,
34        default: Role.user
35    },
36    subscriptions: [subscription.modelName]
37})
38
39// hash user password before saving into database
40userModel.pre('save', async function save(next) {
41    if (!this.isModified('password')) return next();
42    try {
43      const salt = await bcrypt.genSalt(saltRounds);
44      this.password = await bcrypt.hash(this.password, salt);
45      return next();
46    } catch (err) {
47      return next(err);
48    }
49});
50  
51// Compare passwords when user tries to log in.
52userModel.methods.comparePassword = function(candidatePassword, cb) {
53    bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
54        if (err) return cb(err);
55        cb(null, isMatch);
56    });
57};
58
59module.exports = mongoose.model('User', userModel)
60
queries leading to this page
mongoose update subdocuments by idupdate or create subdocument mongoosemongoose js update document in subdocuments collectionmongoose document update with subdocument in nodemongoose change in subdocumentmongoose get items from subdocumentmongoose find subdocument in array by idusing find by id and update mongoosehow to embed subdocument in node jsmongoose update subdocument array objectupdate subdocumetn mongoosemongoose update child documentmongoose update sub documentmongoose update into mongoose subdocumentsmongoose update subdocument by filtermongoose return only ids insertmanymongoose update collection with subdocumentsmongoose update data subdocumentmongoose subdocuments update recordmongoose update with subdocumentsnode js mongoose update subdocumentmongoose update subdocumentmongoose update subdocument examplemongoose update subdocument by idselect subdocument in an array to update 24set in mongoose subdocumentmongoose update subdocument array elementupdate a subdocument mongoosemongoose findbyidandupdate update subdocumentshow to find and update subdocuments id mongooseroute for create subdocument and update parent document mongoose expresshow to update subdocument array in mongoosehow to find and update subdocuments mongooseupdating an embedded subdocument mongoosemongoose find in subschemamongoose update subdocument fieldhow to find and update subdocuments id mongoosehupdate mongoose subdocumente especificoupdating subdocument mongoosemongoose find and update subdocument best practicesupdate express mongoose sub documentsupdate mongoose with subdocumentsatualizar subdocumento mongooseupdate mongoose subdocumentmongoose find one and update subdocumentupdateone return document mongooseupdate sub document mongoose with idmongoose update a subdocumentmongoose get subdocumentmongoose updateone for subdocmenthow to update existing subdocument in mongooseupdate subdocument to object mongoosemongoose subdocuments update specific recordmongoose update by idupsert subdocs mongooseupdate subdocument in mongoosemongoose findone 22subdocument 22 match multiple fieldsupdate subdocuments mongoosemongoose how to update array of subdocumentsmongoose update other document by objectidi am not able to add multiple objects in subdocument in postmanmongoose create document with subdocument of many objects 2c how to update a single object in that subdocumenti am not able to add multiple value in subdocument in postman apimongoose find in array subdocument dataupdate a documnet in mongoose that has a subdocmentupdate subdocument mongoosemongoose updateone for subdocumentmongoose update array of subdocumentsmongoose update subdocument by propertymongoose update subdocument arraymongoose update linked subdocumentmongoose update subdocument to arrayupdate specific subdocument mongooseupdate to subdocument didn 27t update parent documentmongoose findbyidandupdate update embedded subdocumentdhow to update subdocument in mongoosemongoose update attributehow to update sub document mongoosehow to edit child schema in mongodbmongoose update to subdocument by criteriahow to updata a subdoc mongoosemongoose updating subdocumentsupdating elemnt of subdocumentnpm mongoose update subdocumentsmongoose set subdocumentmongoose find and update subdocument arraymongoose controller for subdocument updatemongoose js update document in subdocument collectionmongoose findone and update subdocumentmongoose updateone subdocumentmongoose update element in arrayupdate single subdocument key value using mongodbmongoose add subdocument to document is a patch or postexpress mongoose find and add new subdocumentupdating a subdocument mongoosemongoose update subdocument by id