Given an example document as below is there a way to get a bson string representing the document?
class Foobar
include Mongoid::Document
store_in collection :foobars
field :n, as: :name, type: String
field :ts, as: :install_timestamp, type: DateTime
end
ie
john_smith = Foobar.create(name: "John Smith", install_timestamp: Time.now)
john_smith.bson_string
which would return one of the following
{
"n": "John Smith",
"ts": {"$date": {"$numberLong":"1573596151000"} }
}
or
{
"n": "John Smith",
"ts" : ISODate("2019-11-12T22:02:31.848Z"),
}
I've tried doing this utilizing the underlying BSON library and the to_bson function but that returns a bytebuffer which I can't manage to get converted into a string.
Not completely sure, if this is what you need, but
class Foobar
include Mongoid::Document
store_in collection: :foobars
field :n, as: :name, type: String
field :ts, as: :install_timestamp, type: DateTime
end
john_smith = Foobar.create(name: "John Smith", install_timestamp: Time.now)
# => #<Foobar _id: 5dcca0b9909208042530925a, n(name): "John Smith", ts(install_timestamp): 2019-11-14 00:32:57 UTC>
john_smith_attributes = john_smith.attributes
# => {"_id"=>BSON::ObjectId('5dcca0b9909208042530925a'), "n"=>"John Smith", "ts"=>2019-11-14 00:32:57 UTC}
bson = john_smith_attributes.to_bson
# => #<BSON::ByteBuffer:0x00007fd59d16eb10>
bson_string = bson.to_s
# => "4\x00\x00\x00\a_id\x00]\xCC\xA0\xB9\x90\x92\b\x04%0\x92Z\x02n\x00\v\x00\x00\x00John Smith\x00\tts\x00\xC2\xD5Sgn\x01\x00\x00\x00"
BSON::Document.from_bson(BSON::ByteBuffer.new(bson_string))
# => {"_id"=>BSON::ObjectId('5dcca0b9909208042530925a'), "n"=>"John Smith", "ts"=>2019-11-14 00:32:57 UTC}
Links to docs:
BSON::ByteBuffer#to_s
from_bson and to_bson
attributes
Hope this helps.
Related
In ruby how can i parse a json to an array of objects?
Example: i have 2 classes:
class Person
attr_accessor :name, :address, :email, :address
end
And:
class Address
attr_accessor :street, :city, :state, :person
end
When i make a request i get the following json:
{
"data": [
{
"id": 9111316,
"name": "Mason Lee",
"email": "normanodonnell#biospan.com",
"address": {
"state": "American Samoa",
"street": "Cameron Court",
"city": "Wakulla"
}
},
{
"id": 500019,
"name": "Stella Weeks",
"email": "hansenwhitfield#candecor.com",
"address": {
"state": "Nevada",
"street": "Lake Street",
"city": "Wacissa"
}
}
]
}
This json should be parsed into an array of Person.
For now i'm doing:
#json gem
require 'json'
#...
#parse the json and get the 'data'
parsed_json = JSON.parse json
json_data = parsed_json['data']
objects = Array.new
if json_data.kind_of?(Array)
#add each person
json_data.each { |data|
current_person = Person.new
data.each { |k, v|
current_person.send("#{k}=", v)
}
objects.push(current_person)
}
end
#return the array of Person
objects
I have a lot of objects like the above example and do this parse manually is not desirable. There is an automated way to do this?
By "automated way" i mean something like in java with jackson:
ObjectMapper mapper = new ObjectMapper();
List<Person> myObjects = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Person.class));
You can initialize the Person with the hash:
json_data = JSON.parse(json)['data']
json_data.map do |data|
Person.new data
end
class Person
attr_accessor :name, :email, :address
def initialize params
params.each { |k,v| klass.public_send("#{k}=",v) }
end
end
If you want to choose the class dynamically, you can use:
json_data.map do |data|
klass = 'Person'
klass.get_const.new data
Why not just make the method yourself? Example:
require 'json'
def parse_json_to_class_array(data,root_node,to_klass)
json_data = JSON.parse(data)[root_node]
if json_data.is_a?(Array)
objects = json_data.map do |item|
klass = to_klass.new
item.each { |k,v| klass.public_send("#{k}=",v) }
klass
end
end
objects ||= []
end
Then for your example you could call it like so
json ="{\"data\":[
{\"id\":9111316,
\"name\":\"Mason Lee\",
\"email\":\"normanodonnell#biospan.com\",
\"address\":{
\"state\":\"American Samoa\",
\"street\":\"Cameron Court\",
\"city\":\"Wakulla\"
}
},
{\"id\":500019,
\"name\":\"Stella Weeks\",
\"email\":\"hansenwhitfield#candecor.com\",
\"address\":{
\"state\":\"Nevada\",
\"street\":\"Lake Street\",
\"city\":\"Wacissa\"
}
}
]
}"
class Person
attr_accessor :id, :name,:email, :address
end
parse_json_to_class_array(json,'data',Person)
#=>[#<Person:0x2ede818 #id=9111316, #name="Mason Lee", #email="normanodonnell#biospan.com", #address={"state"=>"American Samoa", "street"=>"Cameron Court", "city"=>"Wakulla"}>,
#<Person:0x2ede7a0 #id=500019, #name="Stella Weeks", #email="hansenwhitfield#candecor.com", #address={"state"=>"Nevada", "street"=>"Lake Street", "city"=>"Wacissa"}>]
Obviously you can expand this implementation to support single objects as well as overwrite Person#address= to perform the same operation and turn the address Hash into an Address object as well but this was not shown in your example so I did not take it this far in my answer.
A more dynamic example can be found Here
I'm having trouble saving the correct nested embedded document with mongoid. I've tried this in both v3.1.6 and v4.0.0 and the result is the same. The parent document is saved, but the nested document I modified ignores the changes, and instead updates the first nested document instead.
Assume some models like so:
Mongoid.configure do |config|
config.sessions = {default: {hosts: ["localhost:27017"], database: "test_mongoid"}}
end
class Band
include Mongoid::Document
field :name, type: String
embeds_many :members
end
class Member
include Mongoid::Document
embedded_in :band
field :name, type: String
embeds_many :instruments
end
class Instrument
include Mongoid::Document
embedded_in :member
field :name, type: String
def make_badass!
self.name = "Badass #{self.name}"
self.save
end
end
And a program that runs:
Band.destroy_all
a_band = {
name: "The Beatles",
members: [
{
name: 'Paul',
instruments: [
{
name: 'Bass guitar'
},
{
name: 'Voice'
}
]
}
]
}
Band.create(a_band)
the_beatles = Band.first
puts the_beatles.as_document
paul = the_beatles.members.first
voice = paul.instruments.where({name: "Voice"}).first
voice.make_badass!
puts Band.first.as_json
The database should now contain:
{
"_id": ObjectId('53aa7d966d6172889c000000'),
"name" : "The Beatles",
"members" : [
{
"_id" : ObjectId('53aa7d966d6172889c010000'),
"name" : "Paul",
"instruments" : [
{"_id" : ObjectId('53aa7d966d6172889c020000'), "name" : "Bass guitar"},
{"_id" : ObjectId('53aa7d966d6172889c030000'), "name" : "Voice"}
]
}
]
}
But instead, it contains:
{
"_id": ObjectId('53aa7d966d6172889c000000'),
"name" : "The Beatles",
"members" : [
{
"_id" : ObjectId('53aa7d966d6172889c010000'),
"name" : "Paul",
"instruments" : [
{"_id" : ObjectId('53aa7d966d6172889c020000'), "name" : "Badass Voice"},
{"_id" : ObjectId('53aa7d966d6172889c030000'), "name" : "Voice"}
]
}
]
}
What would be a working way of having the correct embedded document to change from within the instance method of Instrument?
Thanks for your help!
Mongoid is quickly forcing me to become an alcoholic. Hopefully it will help someone in the same situation.
class Instrument
include Mongoid::Document
embedded_in :member
field :name, type: String
def make_badass
self.name = "Badass #{self.name}"
self.member.band.save
end
def unset_name
# self.unset :name does not work
self.remove_attribute :name
self.member.band.save
end
end
I am reading through this Stackoverflow answer about how to insert multiple documents in Mongoid in one query. From the answer I read:
batch = [{:name => "mongodb"}, {:name => "mongoid"}]
Article.collection.insert(batch)
I need an example to understand how this works. Say we have the Article class:
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :subject, type: String
field :body, type: String
field :remote_id, type: String
validates_uniqueness_of :remote_id
belongs_to :news_paper, :inverse_of => :articles
end
And the I e.g. create an array of articles:
[ {subject: "Mongoid rocks", body: "It really does", remote_id: "1234", news_paper_id: "abc"},
{subject: "Ruby rocks", body: "It really does", remote_id: "1234", news_paper_id: "abc"},
{subject: "Rails rocks", body: "It really does", remote_id: "5678", news_paper_id: "abc"} ]
How do I create them, and at the same time make sure the validation catches that I have 2 remote_id's that are the same?
If you add a unique indexing for remote_id field, MongoDB will take care the uniqueness of this field
index({ remote_id: 1 }, { unique: true })
Don't forget to run create_indexes: rake db:mongoid:create_indexes
After that, you are free to use Article.collection.insert(batch).
I have two simple classes
class Band
include Mongoid::Document
field :name, type:String
has_many :members
end
class Member
include Mongoid::Document
field :name, type: String
belongs_to :band
end
After I have created two object for test purposes
Band.create(title: 'New Band')
Band.members.create(name: 'New Member')
I got next db state:
> db.bands.find()
{ "_id" : ObjectId("..."), "title" : "New Band" }
> db.members.find()
{ "_id" : ObjectId("..."), "name" : "New Member", "band_id" : ObjectId("...") }
When I try to build json object of Band object I get data without children:
{"_id":"...","title":"New Band"}
But I need something like that:
{"_id":"...","title":"New Band", "members" : {"_id":"...","title":"New Member"}}
How to build json with children??
You can override serializable_hash:
class Member
include Mongoid::Document
field :name, type: String
belongs_to :band
def serializable_hash(options={})
{
id: id,
name: name
}
end
end
class Band
include Mongoid::Document
field :title, type: String
has_many :members
def serializable_hash(options={})
{
id: id,
title: title,
members: members.inject([]) { |acc, m| acc << m.serializable_hash; acc }
}
end
end
Suppose you have a band with a member:
band = Band.create(title: 'New Band')
band.members.create(name: 'New Member')
In that case band.to_json will return you something like that:
"{\"id\":...,\"title\":\"New Band\",\"members\":[{\"id\":...,\"name\":\"New Member\"}]}"
Try this:
a_band = Band.last
a_band.as_json(methods: [:members])
Mongoid auto-generates helper methods for your relations, and you can include these methods when you build your JSON object. You can use a_band.members to fetch the band's members out of the db, so you can include that method in your JSON object, like any other method on the model.
I looked into different resources and still get confused on how to parse a json format to a custom object, for example
class Resident
attr_accessor :phone, :addr
def initialize(phone, addr)
#phone = phone
#addr = addr
end
end
and JSON file
{
"Resident": [
{
"phone": "12345",
"addr": "xxxxx"
}, {
"phone": "12345",
"addr": "xxxxx"
}, {
"phone": "12345",
"addr": "xxxxx"
}
]
}
what's the correct way to parse the json file into a array of 3 Resident object?
Today i was looking for something that converts json to an object, and this works like a charm:
person = JSON.parse(json_string, object_class: OpenStruct)
This way you could do person.education.school or person[0].education.school if the response is an array
I'm leaving it here because might be useful for someone
The following code is more simple:
require 'json'
data = JSON.parse(json_data)
residents = data['Resident'].map { |rd| Resident.new(rd['phone'], rd['addr']) }
If you're using ActiveModel::Serializers::JSON you can just call from_json(json) and your object will be mapped with those values.
class Person
include ActiveModel::Serializers::JSON
attr_accessor :name, :age, :awesome
def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end
def attributes
instance_values
end
end
json = {name: 'bob', age: 22, awesome: true}.to_json
person = Person.new
person.from_json(json) # => #<Person:0x007fec5e7a0088 #age=22, #awesome=true, #name="bob">
person.name # => "bob"
person.age # => 22
person.awesome # => true
require 'json'
class Resident
attr_accessor :phone, :addr
def initialize(phone, addr)
#phone = phone
#addr = addr
end
end
s = '{"Resident":[{"phone":"12345","addr":"xxxxx"},{"phone":"12345","addr":"xxxxx"},{"phone":"12345","addr":"xxxxx"}]}'
j = JSON.parse(s)
objects = j['Resident'].inject([]) { |o,d| o << Resident.new( d['phone'], d['addr'] ) }
p objects[0].phone
"12345"
We recently released a Ruby library static_struct that solves the issue. Check it out.