mongoid / origin

A Ruby DSL for building MongoDB queries
http://mongoid.org/en/origin/index.html
MIT License
62 stars 29 forks source link

.only and .without #96

Open JamesHarrison opened 10 years ago

JamesHarrison commented 10 years ago

In previous versions of origin, calling .without and .only resulted in a latest-takes-precedence behaviour (.without would strip .only, .only would strip .without). MongoDB cannot include and exclude fields at the same time (error 10053 "You cannot currently mix including and excluding fields. Contact us if this is an issue.", on 2.4.6).

In the current version the behaviour appears to be incompatible with MongoDB. Even the tests expect a result which according to MongoDB should not work, as I discovered when trying to fix this:

1) Origin::Optional#only when #without was called first adds both fields to option
 Failure/Error: expect(selection.options).to eq(

   expected: {:fields=>{"id"=>0, "first"=>1}}
        got: {:fields=>{"first"=>1}}

Given the tests make this expectation thought I should probably check to see if I'm missing something or I've misdiagnosed my issue (the place I'm running into this is after a Mongoid/origin/etc update on an existing Rails app - a default_scope of ->{ without(:field) } is now causing all sorts of issues due to this breaking whenever .only is called eg to get a list of IDs I'm doing .only(:id) which now throws a MongoDB error).