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 6840669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:52:19+00:00 2026-05-26T23:52:19+00:00

I have some application on RoR with Mongodb database. I use Mongoid mapper. Model

  • 0

I have some application on RoR with Mongodb database. I use Mongoid mapper. Model post.rb

class Post
  include Mongoid::Document

  field :title, :type => String
  field :text, :type => String

  embeds_many :comments
end

Model comment.rb

class Comment  
  include Mongoid::Document

  field :name, :type => String
  field :content, :type => String

  embedded_in :post, :inverse_of => :comments
end 

In database this post with some comment has next structure:

{
   "_id": ObjectId("4ecbeacf65430f0cef000003"),
   "comments": {
     "0": {
       "name": "my name",
       "content": "example content",
       "_id": ObjectId("4ecbead365430f0cef000005") 
    },
     "1": {
       "name": "some name",
       "content": "example content",
       "_id": ObjectId("4ecbead665430f0cef000007") 
    },
     "2": {
       "name": "some name",
       "content": "example content",
       "_id": ObjectId("4ecbeada65430f0cef000009") 
    } 
  },
   "text": "example text",
   "title": "example title" 
}

And, for example, in database was a few posts with my comments.
I need to find all posts, where "name": "my name", i.e. I need to find all editable by me posts.

  • 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-26T23:52:19+00:00Added an answer on May 26, 2026 at 11:52 pm

    It should appear as a array of comments instead of a hash. See my example below.

    Also, as per the mongoid docs use the new style field declarations.

    comment.rb:

    class Comment
      include Mongoid::Document
    
      field :name, type: String
      field :content, type: String
    
      embedded_in :post
    end
    

    post.rb:

    class Post
      include Mongoid::Document
    
      field :title, type: String
      field :text, type: String
    
      embeds_many :comments
    end
    

    Rails Console:

    p = Post.new(:title => "title", :text => "post")
    c1 = Comment.new(:name => "Tyler", :comment => "Awesome Comment!")
    c2 = Comment.new(:name => "Joe", :comment => "comment 2")
    p.comments << c1
    p.comments << c2
    p.save
    

    Mongo Console:

    > db.posts.findOne()
    {
        "_id" : ObjectId("4ecd151d096f762149000001"),
    "title" : "title",
    "text" : "post body",
    "comments" : [
                {
            "name" : "Tyler",
            "comment" : "Awesome Comment!",
            "_id" : ObjectId("4ecd1569096f762149000002")
        },
        {
            "name" : "Joe",
            "comment" : "comment 2",
            "_id" : ObjectId("4ecd157f096f762149000003")
        }
    ]}
    

    Then, to do the query you want, which I think was “comments posted by me?”:

    > db.posts.findOne({"comments.name": "Tyler"})
    

    Also, I would add an index to the comments.name field:

    > db.posts.ensureIndex({"comments.name": 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some application A with a database. Now I want to add
I have some text area field in my grails application. I got the following
I have some model objects I'm using in my Java client application. Later these
My first application I have developed in RoR is for some Kiosk Touchscreen PCs
I have some application, which makes database requests. I guess it doesn't actually matter,
We have some issues in our application with skype taking over some of our
In my application I have some link buttons there but when I right click
I have to run some other application from my program and hide it's form.
I have some problem's with a simple application in JSF 2.0. I try to
I have some question: How to make a role based web application? Such as

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.