Skip to content

Commit f273acd

Browse files
p-mongojohnnyshieldsp
authored
MONGOID-5391 improve pluck documentation, add tests (#5399)
* MONGOID-5391 improve documentation for pluck * additional tests Co-authored-by: shields <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 39ee65c commit f273acd

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

docs/reference/queries.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,12 +1412,20 @@ Mongoid also has some helpful methods on criteria.
14121412
.. code-block:: ruby
14131413

14141414
Band.all.pluck(:name)
1415+
#=> ["Daft Punk", "Aphex Twin", "Ween"]
14151416

1416-
Band.all.pluck('cities.name')
1417+
Band.all.pluck('address.city')
1418+
#=> ["Paris", "Limerick", "New Hope"]
14171419

14181420
# Using the earlier definition of Manager,
14191421
# expands out to "managers.name" in the query:
14201422
Band.all.pluck('managers.n')
1423+
#=> [ ["Berry Gordy", "Tommy Mottola"], [], ["Quincy Jones"] ]
1424+
1425+
# Accepts multiple field arguments, in which case
1426+
# the result will be returned as an Array of Arrays.
1427+
Band.all.pluck(:name, :likes)
1428+
#=> [ ["Daft Punk", 342], ["Aphex Twin", 98], ["Ween", 227] ]
14211429

14221430
* - ``Criteria#take``
14231431

lib/mongoid/contextual/mongo.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,22 @@ def map_reduce(map, reduce)
391391
MapReduce.new(collection, criteria, map, reduce)
392392
end
393393

394-
# Pluck the single field values from the database. Will return duplicates
395-
# if they exist and only works for top level fields.
394+
# Pluck the field value(s) from the database. Returns one
395+
# result for each document found in the database for
396+
# the context. The results are normalized according to their
397+
# Mongoid field types. Note that the results may include
398+
# duplicates and nil values.
396399
#
397400
# @example Pluck a field.
398401
# context.pluck(:_id)
399402
#
400-
# @param [ String | Symbol ] *fields Field(s) to pluck.
403+
# @param [ String | Symbol ] *fields Field(s) to pluck,
404+
# which may include nested fields using dot-notation.
401405
#
402406
# @return [ Array<Object> | Array<Array<Object>> ] The plucked values.
407+
# If the *fields arg contains a single value, each result
408+
# in the array will be a single value. Otherwise, each
409+
# result in the array will be an array of values.
403410
def pluck(*fields)
404411
# Multiple fields can map to the same field name. For example, plucking
405412
# a field and its _translations field map to the same field in the database.

spec/mongoid/contextual/memory_spec.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,11 +1243,11 @@
12431243
config_override :legacy_pluck_distinct, true
12441244

12451245
let(:hobrecht) do
1246-
Address.new(street: "hobrecht")
1246+
Address.new(street: "hobrecht", number: 213)
12471247
end
12481248

12491249
let(:friedel) do
1250-
Address.new(street: "friedel")
1250+
Address.new(street: "friedel", number: 11)
12511251
end
12521252

12531253
let(:criteria) do
@@ -1267,6 +1267,17 @@
12671267
end
12681268
end
12691269

1270+
context "when plucking multiple fields" do
1271+
1272+
let!(:plucked) do
1273+
context.pluck(:street, :number)
1274+
end
1275+
1276+
it "returns the values as an array" do
1277+
expect(plucked).to eq([ ["hobrecht", 213], ["friedel", 11] ])
1278+
end
1279+
end
1280+
12701281
context "when plucking a mix of empty and non-empty values" do
12711282

12721283
let(:empty_doc) do
@@ -1296,7 +1307,7 @@
12961307
context.pluck(:foo)
12971308
end
12981309

1299-
it "returns a empty array" do
1310+
it "returns an empty array" do
13001311
expect(plucked).to eq([nil, nil])
13011312
end
13021313
end
@@ -1307,7 +1318,7 @@
13071318
context.pluck(:foo, :bar)
13081319
end
13091320

1310-
it "returns a empty array" do
1321+
it "returns an empty array" do
13111322
expect(plucked).to eq([[nil, nil], [nil, nil]])
13121323
end
13131324
end
@@ -1720,7 +1731,7 @@
17201731
end
17211732
end
17221733

1723-
context "when tallying deeply nested arrays/embedded associations" do
1734+
context "when plucking deeply nested arrays/embedded associations" do
17241735

17251736
let(:criteria) do
17261737
Person.all.tap do |crit|

spec/mongoid/contextual/none_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,17 @@
5454
end
5555

5656
describe "#pluck" do
57-
it "returns an empty array" do
58-
expect(context.pluck(:id)).to eq([])
57+
58+
context "when plucking one field" do
59+
it "returns an empty array" do
60+
expect(context.pluck(:id)).to eq([])
61+
end
62+
end
63+
64+
context "when plucking multiple fields" do
65+
it "returns an empty array" do
66+
expect(context.pluck(:id, :foo)).to eq([])
67+
end
5968
end
6069
end
6170

0 commit comments

Comments
 (0)