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

  • Home
  • SEARCH
  • 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 6082679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:18:59+00:00 2026-05-23T11:18:59+00:00

Need some help with how to use atomic modifiers on an embedded document. To

  • 0

Need some help with how to use atomic modifiers on an embedded document.

To illustrate, let’s assume I’ve got a collection that looks like this.

Posts Collection

{
  "_id" : ObjectId("blah"),
  "title" : "Some title",
  "comments" : [
    {
      "_id" : ObjectId("bleh"),
      "text" : "Some comment text",
      "score" : 0,
      "voters" : []
    }
  ]
}

What I’m looking to do with MongoMapper/MongoDB is perform an atomic update on a specific comment within a post document.

Something like:

class Comment
  include MongoMapper::EmbeddedDocument

  # Other stuff...

  # For the current comment that doesn't have the current user voting, increment the vote score and add that user to the voters array so they can't vote again
  def upvote!(user_id)
    collection.update({"comments._id" => post_id, "comments.voters" => {"$ne" => user_id}}, 
      {"$inc" => {"comments.score" => 1}, "$push" => {"comments.voters" => user_id}})
  end
end

That’s basically what I have now and it isn’t working at all (nothing gets updated). Ideally, I’d also want to reload the document / embedded document but it seems as though there may not be a way to do this using MongoMapper’s embedded document. Any ideas as to what I’m doing wrong?

  • 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-23T11:19:00+00:00Added an answer on May 23, 2026 at 11:19 am

    Got this working for anyone that’s interested. Two things I was missing

    1. Using $elemMatch to search objects within an array that need to satisfy two conditions (such as _id = “” AND voters DOES NOT contain the user_id)
    2. Using the $ operator on the $inc and $push operations to ensure I’m modifying the specific object that’s referenced by my query.

    def upvote!(user_id)
      # Use the Ruby Mongo driver to make a direct call to collection.update
      collection.update(
        {
          'meanings' => {
            '$elemMatch' => {
              '_id' => self.id,
              'voters' => {'$ne' => user_id}
            }
          }
        },
        {
          '$inc' => { 'meanings.$.votes' => 1 },
          '$push' => { 'meanings.$.voters' => user_id }
        })
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with vocabulary, I don't use Oracle that often but I
Need some help assigning a mouseover event to display some icons that start out
i need some help with c# and nhibernate. I'm working on a project that
I need some help in combining a stored procedure with another function. I've got
I need some help. In my android app I have a ViewFlipper that contains
I believe that I am having a bad database design and need some help
I need some help - I am trying to use a custom validation attribute
Okay, I need some help here. This is the same old "can't use an
need some help! I'm trying to write some code in objective-c that requires part-of-speech
I could really use some help with files (images) download/save application process. I need

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.