Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrongly formated ids when using mongoid head #354

Closed
sphw opened this issue Jul 13, 2013 · 12 comments
Closed

Wrongly formated ids when using mongoid head #354

sphw opened this issue Jul 13, 2013 · 12 comments

Comments

@sphw
Copy link

sphw commented Jul 13, 2013

I am upgrade my application to rails 4 and I have noticed that the id field is being formatted wrong. it is being formated like this
"_id": {
"$oid": "51c7bd98e77989d1f1000002"
}
I believe this stems from mongoid returning a json object now instead of simple a string .

@brentkirby
Copy link

I literally just finished dealing with this so thought I'd respond :) This is actually a Mongoid / Moped thing and intentional, to match what Mongo does natively.

You'll need to handle this yourself for the time being ( not sure if they are going to do anything different or not ). What I did was to just override .to_json and .as_json on Moped::BSON::ObjectId in a Rails initializer.

module Moped
  module BSON
    class ObjectId
      alias :to_json :to_s
      alias :as_json :to_s
    end
  end
end

This was also the suggestion offered by Durran. You can find more info here

@mrrooijen
Copy link

@brentkirby 👍 now I can clean up my code again.

@lukaszx0
Copy link
Member

If I understand correctly that's not AMS issue and this can be closed.

@spastorino
Copy link
Contributor

/cc @arthurnn

@arthurnn
Copy link
Contributor

@brentkirby is right. On mongoid the default JSON output for Object id is: {"$oid": "...."} so if you wanna change that behaviour you would have to monkey patch the ObjectId class, something like this:
Mongoid 3.x:

module Moped
  module BSON
    class ObjectId
      alias :to_json :to_s
      alias :as_json :to_s
    end
  end
end

Mongoid 4:

module BSON
  class ObjectId
    alias :to_json :to_s
    alias :as_json :to_s
  end
end

That should be enough to solve this issue.
Thanks for the ticket anyways.

karmi added a commit to elastic/elasticsearch-rails that referenced this issue Dec 11, 2013
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
karmi added a commit to elastic/elasticsearch-rails that referenced this issue Jan 19, 2014
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
@mateusdelbianco
Copy link

@arthurnn as_json and to_json can accept arguments. The code below worked better for me.

module BSON
  class ObjectId
    def as_json(*args)
      to_s
    end

    alias :to_json :as_json
  end
end

@barodeur
Copy link
Contributor

@mateusdelbianco thx, just had a little issue with your code, it produced a JSON like this for me which was invalid :

{
  "id":5123bf6c104729020000004f
  ...
}

I had to add a to_json call to the string.

module BSON
  class ObjectId
    def as_json(*args)
      to_s.to_json
    end

    alias :to_json :as_json
  end
end

@jstejada
Copy link

@mateusdelbianco @barodeur this is really helpful, thanks! Also, you can actually just override #as_json without the aliases:

module BSON
  class ObjectId

    def as_json(*args)
      to_s
    end

  end
end

This will produce proper json output since #to_json jsonifyies whatever #as_json returns, so it will add the proper quotes to the string, and #as_json will return just a regular string as intended.

@dangalg
Copy link

dangalg commented Apr 19, 2015

What if I want the exact opposite?

from:
"_id": "51c7bd98e77989d1f1000002"

to:
"_id": {
"$oid": "51c7bd98e77989d1f1000002"
}

@dfabreguette
Copy link

Just had an issue using elasticsearch mongoid gem (calling rake es:reindex). After Some Search I found this github issue solving my problem : nesquena/backburner#80

@jagdeepsingh
Copy link

Configuration

# Gemfile
gem 'rails', '~> 5.2.0'
gem 'mongoid', '~> 7.0.1'
gem 'bson_ext', '~> 1.5.1'
gem 'active_model_serializers', '~> 0.10.7'

mongoid document serialized using ActiveModel::Serializer

Default

Output

{
    "id": {
        "$oid": "5b336e9d6120b24c62287e5b"
    },
    "token": "823725b2eeb8ced8bed3c58fe18479d1d07d616c072dc02a0e",
    "refresh_token": null,
    "expires_in": 900,
    "revoked_at": null,
    "resource_owner_id": {
        "$oid": "59c8a5bd6120000c4e000164"
    }
}

With initializer

# config/initializers/bson.rb
module BSON
  class ObjectId
    alias :as_json :to_s
  end
end

Output

{
    "id": "5b336e9d6120b24c62287e5b",
    "token": "823725b2eeb8ced8bed3c58fe18479d1d07d616c072dc02a0e",
    "refresh_token": null,
    "expires_in": 900,
    "revoked_at": null,
    "resource_owner_id": "59c8a5bd6120000c4e000164"
}

@rrecio
Copy link

rrecio commented Sep 7, 2018

The solution @jstejada posted worked better for me:

module BSON
  class ObjectId

    def as_json(*args)
      to_s
    end

  end
end

The other solutions crashed due to lack of *args or generated id strings containing special quote character at the begining and end of string, which led my REST consumer code to read id field as "\"123131b23b1b23b1b3\"" instead of the expected "123131b23b1b23b1b3"

oleksandrbyk added a commit to oleksandrbyk/olek-elastic-rails that referenced this issue Feb 6, 2019
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
SuperFullStack added a commit to SuperFullStack/elasticsearch_rails that referenced this issue Dec 8, 2021
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
Durorexp added a commit to Durorexp/elasticsearch-rails that referenced this issue Mar 14, 2022
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
koziiroman added a commit to koziiroman/Elasticsearch-Integraion that referenced this issue Apr 4, 2022
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
patrickm53 pushed a commit to patrickm53/search-rails-on-rails that referenced this issue Sep 23, 2022
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
richardstewart0213 added a commit to richardstewart0213/rails-elasticsearch that referenced this issue Nov 4, 2022
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
mikejohn857 added a commit to mikejohn857/elasticsearch-rails-per-sistence that referenced this issue Nov 25, 2022
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
superdev9082 added a commit to superdev9082/elasticsearch-rails that referenced this issue Feb 16, 2023
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
bluedone added a commit to bluedone/elasticsearch-rails that referenced this issue Jun 6, 2023
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
miniTalDev added a commit to miniTalDev/elasticsearch-rails that referenced this issue Aug 4, 2023
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
pemilas added a commit to pemilas/elastic-search-rails that referenced this issue Aug 18, 2023
…xed_json` method

Default Mongoid serialization keeps the `_id` property in the JSON as "BSON Object",
which breaks Elasticsearch.

See:

* elastic/elasticsearch#3517
* rails-api/active_model_serializers#354 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests