Is there a YAML Database? Is there a YAML Database? mongodb mongodb

Is there a YAML Database?


Or is there a mapper from YAML to something like Mongo DB that lets me transparently use it as a YAML store?

Not directly as far as I know.

The main problem seems to be that Mongo DB's interface is based on a hash (i.e. key-value pairs). So in essence you are asking if there is a mapper from YAML to a hash. The answer is no simply because YAML can store arbitrary structures while hashes cannot.

But if you're willing to simplify your objects, you might be able to partially do this:

class SomeObject  def initialize    @bob = 'abc'    @fred = 'cde'  end  def to_hash    h = {}    instance_variables.sort.each do |v|    h[v] = instance_variable_get(v)  end  return hend

If you look at the to_yaml code you'll see that it's very similar to the to_hash method (because that's where I got the idea).

Note you'll probably need a from_hash method as well. And you probably want to start working out a scheme for classes with instance variables that are objects (i.e. not just Strings, Symbols, etc.)

Now the mongo code to insert the item:

include Mongodb = MongoClient.new.db('test')stuff = db.collection('stuff')item = SomeClass.new()... other code ...stuff.insert(item.to_hash)

I realize this doesn't answer your question directly, but hopefully it still helps.

John


Updating the topic, take a look at: http://blog.varunajayasiri.com/yamldb.

"YAML database is a document database which stores documents as YAML files. The documents in the database can be maintained by simply editing the yaml files.

This database was designed to be used for systems like CMS systems, where an easy way to edit data is necessary and the number of data objects is not very high. It can be also used to store settings and configurations.

Storing the database as separate files lets you use version control systems like git on the database, which is again ideal for storing settings, configurations, blog posts and CMS content."


Yaml is rather used for config purposes. It is not used for database such as MongoDB because serialization takes longer than json. In fact, json is not a subset of yaml (although it's close). Json libraries are generally faster : stackoverflow.com/questions/2451732/….

If interoperability and speed are a concern, use JSON. - Erik Aronesty