Skip to content

Commit

Permalink
Add pass document parameter to unwind method support since new specif…
Browse files Browse the repository at this point in the history
…ication in MongoDB version 3.2.
  • Loading branch information
chenzww committed Jun 9, 2017
1 parent 95a25a7 commit c94fc1a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/mongoid/criteria/queryable/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,26 @@ def project(entry)
#
# @example Add the unwind.
# pipeline.unwind(:field)
# pipeline.unwind(document)
#
# @see https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
#
# @param [ String, Symbol ] field The name of the field.
# @param [ Hash ] document.
#
# @return [ Pipeline ] The pipeline.
#
# @since 2.0.0
def unwind(field)
normalized = field.to_s
name = aliases[normalized] || normalized
push("$unwind" => name.__mongo_expression__)
unless field.respond_to? :keys
normalized = field.to_s
name = aliases[normalized] || normalized
push("$unwind" => name.__mongo_expression__)
else
permit_keys = ['path', 'includeArrayIndex', 'preserveNullAndEmptyArrays'].freeze
normalized = BSON::Document.new(field.select { |k| permit_keys.include?(k.to_s) })
push("$unwind" => normalized)
end
end

private
Expand Down
14 changes: 14 additions & 0 deletions spec/mongoid/criteria/queryable/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,19 @@
end
end
end

context "when provided a hash value" do
context "unnecessary keys should be removed" do
before do
pipeline.unwind(path: "$foo", "includeArrayIndex" => "index", preserveNullAndEmptyArrays: true, foo: "bar")
end

it "only reserve permit keys" do
expect(pipeline).to eq([
{ "$unwind" => { "path" => "$foo", "includeArrayIndex" => "index", "preserveNullAndEmptyArrays" => true } }
])
end
end
end
end
end

0 comments on commit c94fc1a

Please sign in to comment.