<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -15,13 +15,13 @@ module DataMapper
         options[:limit] = @query.limit if @query.limit
         options[:sort]  = sort_statement(@query.order) unless @query.order.empty?
 
-        condition_statement(@query.conditions)
+        conditions_statement(@query.conditions)
 
         @collection.find(@statements, options).to_a
       end
 
       private
-        def condition_statement(conditions, affirmative = true)
+        def conditions_statement(conditions, affirmative = true)
           case conditions
             when AbstractOperation  then operation_statement(conditions, affirmative)
             when AbstractComparison then comparison_statement(conditions, affirmative)
@@ -30,8 +30,8 @@ module DataMapper
 
         def operation_statement(operation, affirmative = true)
           case operation
-            when NotOperation then condition_statement(operation.first, !affirmative)
-            when AndOperation then operation.each{|op| condition_statement(op, affirmative)}
+            when NotOperation then conditions_statement(operation.first, !affirmative)
+            when AndOperation then operation.each{|op| conditions_statement(op, affirmative)}
             when OrOperation  then raise NotImplementedError
           end
         end
@@ -39,6 +39,10 @@ module DataMapper
         #--
         # TODO: Rather than raise an error do what we can in a $where operation or in memory.
         def comparison_statement(comparison, affirmative = true)
+          if comparison.relationship?
+            return conditions_statement(comparison.foreign_key_mapping, affirmative)
+          end
+
           field = comparison.subject.field
           value = comparison.value
 
@@ -62,7 +66,9 @@ module DataMapper
             end
           end
 
-          @statements.update(operator.is_a?(Hash) &amp;&amp; operator.has_key?('$where') ? operator : {field.to_sym =&gt; operator})
+          statement = operator.is_a?(Hash) &amp;&amp; operator.has_key?('$where') ? operator : {field.to_sym =&gt; operator}
+
+          @statements.update(statement)
         end
 
         def sort_statement(conditions)</diff>
      <filename>lib/dm-mongo-adapter/query.rb</filename>
    </modified>
    <modified>
      <diff>@@ -22,4 +22,83 @@ describe DataMapper::Adapters::MongoAdapter do
   end
 
   it_should_behave_like 'An Adapter'
+
+  describe &quot;associations&quot; do
+    before :all do
+      class User
+        include DataMapper::Resource
+
+        property :id, DataMapper::Mongo::Types::ObjectID
+        property :group_id, DataMapper::Mongo::Types::ObjectID, :field =&gt; 'group_id'
+        property :name, String
+        property :age, Integer
+      end
+
+      class Group
+        include DataMapper::Resource
+
+        property :id, DataMapper::Mongo::Types::ObjectID
+        property :name, String
+      end
+
+      User.belongs_to :group
+      Group.has Group.n, :users
+    end
+
+    before :each do
+      @db.drop_collection('users')
+      @db.drop_collection('groups')
+
+      @john = User.create(:name =&gt; 'john', :age =&gt; 101)
+      @jane = User.create(:name =&gt; 'jane', :age =&gt; 102)
+
+      @group = Group.create(:name =&gt; 'dm hackers')
+    end
+
+    describe &quot;belongs_to&quot; do
+      it &quot;should set parent object _id&quot; do
+        lambda {
+          @john.group = @group
+          @john.save
+        }.should_not raise_error
+
+        @john.group_id.should eql(@group.id)
+      end
+
+      it &quot;should fetch parent object&quot; do
+        user = User.create(:name =&gt; 'jane')
+        user.group_id = @group.id
+        user.group.should eql(@group)
+      end
+    end
+
+    describe &quot;has many&quot; do
+      before :each do
+        [@john, @jane].each { |user| user.update(:group_id =&gt; @group.id) }
+      end
+
+      it &quot;should get children&quot; do
+        @group.users.size.should eql(2)
+      end
+
+      it &quot;should add new children with &lt;&lt;&quot; do
+        user = User.new(:name =&gt; 'kyle')
+        @group.users &lt;&lt; user
+        user.group_id.should eql(@group.id)
+        @group.users.size.should eql(3)
+      end
+
+      it &quot;should replace children&quot; do
+        user = User.create(:name =&gt; 'stan')
+        @group.users = [user]
+        @group.users.size.should eql(1)
+        @group.users.first.should eql(user)
+      end
+
+      it &quot;should fetch children matching conditions&quot; do
+        users = @group.users.all(:name =&gt; 'john')
+        users.size.should eql(1)
+      end
+    end
+  end
 end</diff>
      <filename>spec/adapter_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>2bf39dac7c39fa9d776670f0853bd98969cf22a5</id>
    </parent>
  </parents>
  <author>
    <name>Piotr Solnica</name>
    <email>piotr@zenbe.com</email>
  </author>
  <url>http://github.com/solnic/dm-mongo-adapter/commit/98563bf27a25eb995c587d920726004f8d12c83a</url>
  <id>98563bf27a25eb995c587d920726004f8d12c83a</id>
  <committed-date>2009-11-02T13:28:43-08:00</committed-date>
  <authored-date>2009-11-02T13:28:43-08:00</authored-date>
  <message>added initial support for &quot;belongs to&quot; and &quot;has many&quot; associations</message>
  <tree>c38a4403d985a7f4d5b51b7d6229c493a472092a</tree>
  <committer>
    <name>Piotr Solnica</name>
    <email>piotr@zenbe.com</email>
  </committer>
</commit>
