Skip to content

Commit 0e07881

Browse files
committed
DOCS-1237 tutorial for push operation w modifiers
1 parent 43cb7c2 commit 0e07881

File tree

5 files changed

+118
-13
lines changed

5 files changed

+118
-13
lines changed

source/applications.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ The following documents provide patterns for developing application features:
5757
tutorial/isolate-sequence-of-operations
5858
tutorial/create-an-auto-incrementing-field
5959
tutorial/expire-data
60+
tutorial/limit-number-of-elements-in-updated-array

source/reference/aggregation/match.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ $match (aggregation)
8585
<index>` like any other :mongodb:method:`db.collection.find()`
8686
or :mongodb:method:`db.collection.findOne()`.
8787

88-
.. versionchanged:: 2.4
89-
Prior to 2.4, :pipeline:`$match` queries did **not** support the
90-
geospatial :mongodb:operator:`$within` operator.
88+
.. versionadded:: 2.4
89+
:pipeline:`$match` queries can support the
90+
geospatial :mongodb:operator:`$within` operations.
9191

9292
.. warning::
9393

source/release-notes/2.4.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -940,20 +940,32 @@ performs an insert, use the :operator:`$setOnInsert` operator with the
940940
update and for :method:`updates <db.collection.update()>` when the
941941
``upsert`` option is ``false``.
942942

943-
``$each``, ``$slice``, and ``$sort`` Available for Use with ``$push``
944-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
943+
Limit Number of Elements in an Array
944+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
945945

946-
You can now use the :operator:`$push` operator with the following
947-
modifiers:
946+
Consider a situation where you only want to maintain a fixed number of
947+
elements in an array.
948948

949-
- :operator:`$each` to append multiple values to the array field,
949+
In 2.4, by using the :operator:`$push` operator with the
950+
:operator:`$each`, the :operator:`$sort`, and the :operator:`$slice`
951+
modifiers, you can add multiple elements to an array, sort and limit
952+
the number of elements in the modified array to maintain an array with
953+
a fixed number of elements.
950954

951-
- :operator:`$slice` to restrict the number of array elements and which
952-
must be used with :operator:`$each`, and
955+
See :doc:`/tutorial/limit-number-of-elements-in-updated-array` for an
956+
example where an update maintains the top three scores for a student.
953957

954-
- :operator:`$sort` to order the elements of the array, which
955-
must be used with :operator:`$slice` and can **only** sort
956-
arrays that contain documents.
958+
.. seealso::
959+
960+
The following pages provide additional information and examples:
961+
962+
- :operator:`$push` operator
963+
964+
- :operator:`$each` modifier
965+
966+
- :operator:`$sort` modifier
967+
968+
- :operator:`$slice` modifier
957969

958970
.. _rn-2.4-javascript-change:
959971

source/tutorial.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Development Patterns
6262
- :doc:`/tutorial/enforce-unique-keys-for-sharded-collections`
6363
- :doc:`/tutorial/aggregation-examples`
6464
- :doc:`/tutorial/model-data-for-keyword-search`
65+
- :doc:`/tutorial/limit-number-of-elements-in-updated-array`
6566

6667
.. index:: tutorials; application development
6768
.. index:: application tutorials
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
====================================================
2+
Limit Number of Elements in an Array after an Update
3+
====================================================
4+
5+
.. default-domain:: mongodb
6+
7+
.. versionadded:: 2.4
8+
9+
Synopsis
10+
--------
11+
12+
Consider an application where users may submit many scores (e.g. for a
13+
test), but the application only needs to track the top three test
14+
scores.
15+
16+
This pattern uses the :operator:`$push` operator with the
17+
:operator:`$each`, :operator:`$sort`, and :operator:`$slice`
18+
modifiers to sort and maintain an array of fixed size.
19+
20+
.. important:: The array elements must be documents in order to use the
21+
:operator:`$sort` modifier.
22+
23+
Pattern
24+
-------
25+
26+
Consider the following document in the collection ``students``:
27+
28+
.. code-block:: javascript
29+
30+
{
31+
_id: 1,
32+
scores: [
33+
{ attempt: 1, score: 10 },
34+
{ attempt: 2 , score:8 }
35+
]
36+
}
37+
38+
The the following update uses the :operator:`$push` operator with:
39+
40+
- the :operator:`$each` modifier to append to the array 2 new elements,
41+
42+
- the :operator:`$sort` modifier to order the elements by ascending
43+
(``1``) score, and
44+
45+
- the :operator:`$slice` modifier to keep the last ``3`` elements of
46+
the ordered array.
47+
48+
.. code-block:: javascript
49+
50+
db.students.update(
51+
{ _id: 1 },
52+
{ $push: { scores: { $each : [
53+
{ attempt: 3, score: 7 },
54+
{ attempt: 4, score: 4 }
55+
],
56+
$sort: { score: 1 },
57+
$slice: -3
58+
}
59+
}
60+
}
61+
)
62+
63+
.. note::
64+
65+
When using the :operator:`$sort` modifier on the array element,
66+
access the field in the subdocument element directly instead of
67+
using the :term:`dot notation` on the array field.
68+
69+
After the operation, the document contains the only the top 3 scores in
70+
the ``scores`` array:
71+
72+
.. code-block:: javascript
73+
74+
{
75+
"_id" : 1,
76+
"scores" : [
77+
{ "attempt" : 3, "score" : 7 },
78+
{ "attempt" : 2, "score" : 8 },
79+
{ "attempt" : 1, "score" : 10 }
80+
]
81+
}
82+
83+
.. seealso::
84+
85+
- :operator:`$push` operator,
86+
87+
- :operator:`$each` modifier,
88+
89+
- :operator:`$sort` modifier, and
90+
91+
- :operator:`$slice` modifier.

0 commit comments

Comments
 (0)