<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pivotal Labs &#187; mongodb</title>
	<atom:link href="http://pivotallabs.com/tag/mongodb/feed/" rel="self" type="application/rss+xml" />
	<link>http://pivotallabs.com</link>
	<description>Agility Developed</description>
	<lastBuildDate>Tue, 21 May 2013 02:03:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Mongoid Migrations using the Mongo Driver</title>
		<link>http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/</link>
		<comments>http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/#comments</comments>
		<pubDate>Sun, 30 Jan 2011 02:35:00 +0000</pubDate>
		<dc:creator>Pivotal Labs</dc:creator>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/</guid>
		<description><![CDATA[<p><p>In my <a href="http://pivotallabs.com/users/lee/blog/articles/1523-embedding-mongoid-documents-and-data-migrations">last post</a>, I modified a Mongoid::Document during a migration in order to access fields that where no longer defined in the class. This time I am using the mongo ruby driver directly to migrate data. </p>

<p>Since I am using Mongoid in this project, I will be using it to access the dependent mongo driver. This will prevent me from having to provide mongo with a connection string in my migration. I am also using mongoid_rails_migrations to roll out the change. </p>

<h2>Initial Design</h2>

<pre><code>class User
  include Mongoid::Document
  field :first_name
  field :last_name
end
</code></pre>

<h2>New Design</h2>

<pre><code>class User
  include Mongoid::Document
  field :name
end
</code></pre>

<h2>Migration</h2>

<pre><code>class MergeUsersFirstAndLastName &#60; Mongoid::Migration
  def self.up
    #get the mongo database instance from the Mongoid::Document
    mongo_db = User.db

    #query the collection for the fields needed for the migration
    user_hashes = mongo_db.collection&#40;"users"&#41;.find&#40;{}, :fields =&#62; ["first_name", "last_name"]&#41;

    user_hashes.each do &#124;user_hash&#124;
      new_name = "#{user_hash['first_name']} #{user_hash['last_name']}"

      #update the new field
      mongo_db.collection&#40;"users"&#41;.update&#40;{"_id" =&#62; user_hash["_id"]}, {"$set" =&#62; {"name" =&#62; new_name}}&#41;

      #remove old fields from collection
      mongo_db.collection&#40;"users"&#41;.update&#40;{"_id" =&#62; user_hash["_id"]}, {"$unset" =&#62; { "last_name" =&#62; 1, "first_name" =&#62; 1}}&#41;
    end
  end
end
</code></pre>

<h2>Resources</h2>

<p><a href="http://api.mongodb.org/ruby/current/file.TUTORIAL.html">MongoDB Ruby Driver Tutorial</a></p>

<p><a href="http://www.mongodb.org/display/DOCS/Querying">Querying with MongoDB Ruby Driver</a></p>

<p><a href="http://www.mongodb.org/display/DOCS/Updating">Updating with MongoDB Ruby Driver</a></p> <a href="http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>The post <a href="http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/">Mongoid Migrations using the Mongo Driver</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>In my <a href="http://pivotallabs.com/users/lee/blog/articles/1523-embedding-mongoid-documents-and-data-migrations">last post</a>, I modified a Mongoid::Document during a migration in order to access fields that where no longer defined in the class. This time I am using the mongo ruby driver directly to migrate data. </p>
<p>Since I am using Mongoid in this project, I will be using it to access the dependent mongo driver. This will prevent me from having to provide mongo with a connection string in my migration. I am also using mongoid_rails_migrations to roll out the change. </p>
<h2>Initial Design</h2>
<pre><code>class User
  include Mongoid::Document
  field :first_name
  field :last_name
end
</code></pre>
<h2>New Design</h2>
<pre><code>class User
  include Mongoid::Document
  field :name
end
</code></pre>
<h2>Migration</h2>
<pre><code>class MergeUsersFirstAndLastName &lt; Mongoid::Migration
  def self.up
    #get the mongo database instance from the Mongoid::Document
    mongo_db = User.db

    #query the collection for the fields needed for the migration
    user_hashes = mongo_db.collection&#40;"users"&#41;.find&#40;{}, :fields =&gt; ["first_name", "last_name"]&#41;

    user_hashes.each do |user_hash|
      new_name = "#{user_hash['first_name']} #{user_hash['last_name']}"

      #update the new field
      mongo_db.collection&#40;"users"&#41;.update&#40;{"_id" =&gt; user_hash["_id"]}, {"$set" =&gt; {"name" =&gt; new_name}}&#41;

      #remove old fields from collection
      mongo_db.collection&#40;"users"&#41;.update&#40;{"_id" =&gt; user_hash["_id"]}, {"$unset" =&gt; { "last_name" =&gt; 1, "first_name" =&gt; 1}}&#41;
    end
  end
end
</code></pre>
<h2>Resources</h2>
<p><a href="http://api.mongodb.org/ruby/current/file.TUTORIAL.html">MongoDB Ruby Driver Tutorial</a></p>
<p><a href="http://www.mongodb.org/display/DOCS/Querying">Querying with MongoDB Ruby Driver</a></p>
<p><a href="http://www.mongodb.org/display/DOCS/Updating">Updating with MongoDB Ruby Driver</a></p>
<p>The post <a href="http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/">Mongoid Migrations using the Mongo Driver</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/mongoid-migrations-using-the-mongo-driver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedding Mongoid documents and data migrations</title>
		<link>http://pivotallabs.com/embedding-mongoid-documents-in-migrations/</link>
		<comments>http://pivotallabs.com/embedding-mongoid-documents-in-migrations/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 03:28:00 +0000</pubDate>
		<dc:creator>Pivotal Labs</dc:creator>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[migrations]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/embedding-mongoid-documents-in-migrations/</guid>
		<description><![CDATA[<p><p>When first starting out with mongodb, it's easy to make the wrong decision on whether to embed a document or not.  Even if you made the correct decision at that moment, changing requirements may force you into a migration. So how do you migrate existing data when transitioning from  a standalone document to an embedded document? This is what I came up with. </p>

<h2>Initial Data Structure</h2>

<pre><code>class User
  include Mongoid::Document
  field :name
  references_many :sales
end

class Sale
  include Mongoid::Document
  field :price, :type =&#62; Integer
  referenced_in :user
end
</code></pre>

<h2>Now with Sale embedded in User</h2>

<pre><code>class User
  include Mongoid::Document
  field :name
  embeds_many :sales
end

class Sale
  include Mongoid::Document
  field :price, :type =&#62; Integer
  embedded_in :user, :inverse_of =&#62; :sales
end
</code></pre>

<h2>Migrating Sales Data</h2>

<pre><code>class EmbedSalesInUsers &#60; Mongoid::Migration
  def self.up

    # pull your existing data into memory
    # consider batching for large data sets
    # Note that you must call query methods on the object you are migrating
    # for this method to work &#40;i.e. you can not pull via User#sales&#41;

    sales_attributes = while_stand_alone_doc&#40;Sale&#41; do
      Sale.all.map&#40;&#38;:attributes&#41;
    end

    # now when you save your data, your fields will be embedded

    sales_attributes.each do &#124;attributes&#124;
      user = User.find&#40;attributes[:user_id]&#41;
      user.sales &#60;&#60; Sale.new&#40;:price =&#62; attributes[:price]&#41;
    end

    # remove all the documents from the original collection

    while_stand_alone_doc&#40;Sale&#41; do
      Sale.destroy_all
    end
  end

  def self.while_stand_alone_doc&#40;klass&#41;
    # by changing the Mongoid::Document.embedded you can temporarily
    # modify which collection Mongoid looks to for your model's data store

    begin
      klass.embedded = false

      yield
    ensure
      klass.embedded = true
    end
  end

end
</code></pre>

<p>There are a couple things to note here.</p>

<ul>
<li>The embedded flag in Mongoid::Document is not documented so it could easily change. This was working as of 2.0.0.beta.20</li>
<li>When you create the new embedded document, make sure you pass only the attributes you care about. Passing all attributes will add things that you no longer need like user_id in this case. &#40;For clarity, attributes you assign will be persisted, though you will only have setters and getters for the fields you explicitly define in your document.</li>
<li>I am using mongoid_rails_migrations in this example</li>
</ul> <a href="http://pivotallabs.com/embedding-mongoid-documents-in-migrations/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>The post <a href="http://pivotallabs.com/embedding-mongoid-documents-in-migrations/">Embedding Mongoid documents and data migrations</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>When first starting out with mongodb, it&#8217;s easy to make the wrong decision on whether to embed a document or not.  Even if you made the correct decision at that moment, changing requirements may force you into a migration. So how do you migrate existing data when transitioning from  a standalone document to an embedded document? This is what I came up with. </p>
<h2>Initial Data Structure</h2>
<pre><code>class User
  include Mongoid::Document
  field :name
  references_many :sales
end

class Sale
  include Mongoid::Document
  field :price, :type =&gt; Integer
  referenced_in :user
end
</code></pre>
<h2>Now with Sale embedded in User</h2>
<pre><code>class User
  include Mongoid::Document
  field :name
  embeds_many :sales
end

class Sale
  include Mongoid::Document
  field :price, :type =&gt; Integer
  embedded_in :user, :inverse_of =&gt; :sales
end
</code></pre>
<h2>Migrating Sales Data</h2>
<pre><code>class EmbedSalesInUsers &lt; Mongoid::Migration
  def self.up

    # pull your existing data into memory
    # consider batching for large data sets
    # Note that you must call query methods on the object you are migrating
    # for this method to work &#40;i.e. you can not pull via User#sales&#41;

    sales_attributes = while_stand_alone_doc&#40;Sale&#41; do
      Sale.all.map&#40;&amp;:attributes&#41;
    end

    # now when you save your data, your fields will be embedded

    sales_attributes.each do |attributes|
      user = User.find&#40;attributes[:user_id]&#41;
      user.sales &lt;&lt; Sale.new&#40;:price =&gt; attributes[:price]&#41;
    end

    # remove all the documents from the original collection

    while_stand_alone_doc&#40;Sale&#41; do
      Sale.destroy_all
    end
  end

  def self.while_stand_alone_doc&#40;klass&#41;
    # by changing the Mongoid::Document.embedded you can temporarily
    # modify which collection Mongoid looks to for your model's data store

    begin
      klass.embedded = false

      yield
    ensure
      klass.embedded = true
    end
  end

end
</code></pre>
<p>There are a couple things to note here.</p>
<ul>
<li>The embedded flag in Mongoid::Document is not documented so it could easily change. This was working as of 2.0.0.beta.20</li>
<li>When you create the new embedded document, make sure you pass only the attributes you care about. Passing all attributes will add things that you no longer need like user_id in this case. &#40;For clarity, attributes you assign will be persisted, though you will only have setters and getters for the fields you explicitly define in your document.</li>
<li>I am using mongoid_rails_migrations in this example</li>
</ul>
<p>The post <a href="http://pivotallabs.com/embedding-mongoid-documents-in-migrations/">Embedding Mongoid documents and data migrations</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/embedding-mongoid-documents-in-migrations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android Mongo Meetup</title>
		<link>http://pivotallabs.com/android-mongo-meetup/</link>
		<comments>http://pivotallabs.com/android-mongo-meetup/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 16:39:00 +0000</pubDate>
		<dc:creator>Glenn Jahnke</dc:creator>
				<category><![CDATA[Labs]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://pivotallabs.com/android-mongo-meetup/</guid>
		<description><![CDATA[<p><p>There is meeting tonight at 7pm in San Francisco about using MongoDB for mobile backend servers. Will Shulman from MongoLab will be giving the talk. They will also be offering food and prizes to attendees.</p>

<p>More details can be found at:</p>

<p><a href="http://www.sfandroid.org/calendar/14367370/?eventId=14367370&#38;action=detail" title="SF Android Users Group">http://www.sfandroid.org/calendar/14367370/?eventId=14367370&#38;action=detail</a></p> <a href="http://pivotallabs.com/android-mongo-meetup/">Continue reading <span class="meta-nav">&#8594;</span></a></p><p>The post <a href="http://pivotallabs.com/android-mongo-meetup/">Android Mongo Meetup</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>There is meeting tonight at 7pm in San Francisco about using MongoDB for mobile backend servers. Will Shulman from MongoLab will be giving the talk. They will also be offering food and prizes to attendees.</p>
<p>More details can be found at:</p>
<p><a href="http://www.sfandroid.org/calendar/14367370/?eventId=14367370&amp;action=detail" title="SF Android Users Group">http://www.sfandroid.org/calendar/14367370/?eventId=14367370&amp;action=detail</a></p>
<p>The post <a href="http://pivotallabs.com/android-mongo-meetup/">Android Mongo Meetup</a> appeared first on <a href="http://pivotallabs.com">Pivotal Labs</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://pivotallabs.com/android-mongo-meetup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using apc
Database Caching 1/12 queries in 0.023 seconds using apc
Object Caching 572/604 objects using apc

 Served from: pivotallabs.com @ 2013-05-20 21:21:58 by W3 Total Cache -->