Serializer with MongoDB

Hi
I am creating Ruby on Rails API with mongotDB.
Need suggestion for serialize the object any gem for this.
Thanks in Advance.

Hi @hanish_jadala,

Need suggestion for serialize the object any gem for this.

Can you clarify what it is exactly you’re looking to accomplish? Are you looking to read a document from a MongoDB cluster then serialize the result as JSON and return it via a controller method?

The more information you can share about your use case the easier it will be to respond :wink:

1 Like

Mongoid supports this via custom fields: https://docs.mongodb.com/mongoid/master/tutorials/mongoid-documents/#custom-fields

1 Like

Yes, what you said is right. I want to read document from MongoDB and then serialize the result a JSON and return it via controller method.
I was looking for any gem which help to accomplish this process. When I use PostgreSQL I use active_model_serializers and it helped in great deal. Same way looking for MongoDB.

Mongoid includes ActiveModel, which by extension includes ActiveSupport. As a result, Mongoid documents can leverage Active Support Core Extensions including JSON support.

For example:

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'mongoid'
  gem 'mongo'
end

Mongoid.configure do |config|
  config.clients.default = { uri: "mongodb+srv://..." }
end

class Person
  include Mongoid::Document
  field :name, type: String
end

Person.create(name: "Alex")
puts Person.last.to_json
# => {"_id":{"$oid":"605090f9ceb02323d23116e8"},"name":"Alex"}

Note the result will be emitted as MongoDB Extended JSON (v2).

Thank for your time.

Now Person have 15 columns and I need not return all the 15 column in that case how can I handle it.

If I want to some changes on top of column
Eg: Attribute called amount which is float type and I want to return as round(2) on it.
How can I do it with mongo.

Now Person have 15 columns and I need not return all the 15 column in that case how can I handle it.

Have a look at the documentation for the to_json method for details on how to format the results.

For example:

class Person
  include Mongoid::Document
  field :name, type: String
  field :a, type: Integer, default: 1
  field :b, type: Integer, default: 2
end

p = Person.create(name: "Alex")
puts p.to_json
# => {"_id":{"$oid":"6050af50ceb023bf7eebe776"},"a":1,"b":2,"name":"Alex"}
puts p.to_json(only: :name)
# => {"name":"Alex"}
puts p.to_json(except: [:a, :b])
# => {"_id":{"$oid":"6050af50ceb023bf7eebe776"},"name":"Alex"}

If I want to some changes on top of column

See Oleg’s response regarding Custom Fields.

Using a Custom Field you can define how the value is “mongoized” and “demongoized”, or stored and retrieved.

1 Like

In this case I have to do the changes on every response. Do we have any chance like active-model-serializer, you create a serializer file for model and allow only specific column and also do the calculations on top of the specific column and also add extra keys and in response when we pass model object everything goes according to the serializer file
…

You may be able to just use the active_model_serializers gem directly in your Rails project then.

For example, if starting a new application based on the Mongoid Rails Tutorial try the following:

  1. Update the Gemfile
gem "active_model_serializers"
  1. Run bundle install
  2. Generate a serializer for the Post model (rails g serializer post)
  3. Edit the generated app/serializers/post_serializer.rb:
class PostSerializer < ActiveModel::Serializer
  attributes :id, :title
  def id
    object.id.to_s
  end
end
  1. Edit app/controllers/posts_controller.rb
class PostsController < ApplicationController
  before_action :set_post, only: %i[ show edit update destroy ]
  # GET /posts or /posts.json
  def index
    @posts = Post.all
    respond_to do |format|
      # adding the renderer is needed here
      format.json { render json: @posts, root: false }
    end
  end
  1. Access the JSON route at http://localhost:3000/posts.json

The result should show the list of posts with only a string _id and the title.

1 Like

Model:

class FrequencyDay
  include Mongoid::Document
  include Mongoid::Timestamps
  field :title, type: String
  field :no_of_day, type: Integer
  field :type_of, type: Boolean
end

class FrequencyDaySerializer < ActiveModel::Serializer
  attributes :id, :title, :no_of_day, :type_of, :dummy

  def dummy
    "Dummy"
  end
end

Response:

    "response": {
        "_id": {
            "$oid": "60530408203d565918dc80e7"
        },
        "created_at": "2021-03-18T13:10:56.558+05:30",
        "no_of_day": 6,
        "title": null,
        "type_of": false,
        "updated_at": "2021-03-18T13:10:56.558+05:30"
    }

As you can see I have given only id, :title, :no_of_day, :type_of, :dummy
I can see the time stamps and well can’t see the extra attribute which I have added in serializer.

Am using Ruby 3.0.0 and Rails 6.1.3

After added

root: false

in response things are fine.

Thanks you for your time.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.