Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6921555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:17:32+00:00 2026-05-27T10:17:32+00:00

I have a problem with embedded document update. My defined Schemas: var Talk =

  • 0

I have a problem with embedded document update.

My defined Schemas:

var Talk = new Schema({
    title: {
        type: String,
        required: true
    },
    content: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        required: true
    },
    comments: {
        type: [Comments],
        required: false
    },
    vote: {
        type: [VoteOptions],
        required: false
    },
});

var VoteOptions = new Schema({
    option: {
        type: String,
        required: true
    },
    count: {
        type: Number,
        required: false
    }
});

Now I would like to update vote.count++, with given Talk id and VoteOption id. I have the following function to do the job:

function makeVote(req, res) {

    Talk.findOne(req.params.id, function(err, talk) {
        for (var i = 0; i < talk.vote.length; i++) {
            if (talk.vote[i]._id == req.body.vote) {
                talk.vote[i].count++;

            }
        }
        talk.save(function(err) {
            if (err) {
                req.flash('error', 'Error: ' + err);
                res.send('false');
            } else {
                res.send('true');
            }
        });
    });
}

Everything executes, I get back the res.send('true'), but the value on count does not change.

When I did some console.log I saw that it changed the value, but the talk.save just doesn’t save it in db.

Also I’m quite unhappy about the cycle just to find _id of embedded doc. In the mongoose documentation I read about talk.vote.id(my_id) but that gives me error of not having an id function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:17:32+00:00Added an answer on May 27, 2026 at 10:17 am

    When updating a Mixed type (which seems to be anything else than a basic type, so that also includes embedded documents), one has to call .markModified on the document. In this case, it would be:

    talk.markModified("vote"); // mention that `talk.vote` has been modified
    
    talk.save(function(err) {
        // ...
    });
    

    Hope this helps someone in the future since I couldn’t find the answer very quickly.


    Reference:

    … Mongoose loses the ability to auto detect/save those changes. To “tell” Mongoose that the value of a Mixed type has changed, call the .markModified(path) method of the document passing the path to the Mixed type you just changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with relationships persisting in an embedded document. Here is the
Problem: I have a 'Group' collection. Each group has an embedded document of 'Members'.
I have a CSV string that is embedded within an XML document, which is
I have a problem linking an application for an embedded target. I'm developing on
I have a flash app embedded in a yui TabView tab div, the problem
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have a document with an embedded document in it. I have 2 forms,
In my application I have an embedded MSHTML control. The problem is that I
I have the following problem with XSLT. In an XML document I have several
I have a problem. I'm learning JPA. I'm using embedded OpenEJB container in unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.