Skip to content

Addded Examples for $pull'ing Objects #1863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions source/reference/operator/update/pull.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ After the operation, the documents no longer have ``"msr"`` values:
{ _id: 1, flags: [ "vme", "de", "tsc", "pse" ] }
{ _id: 2, flags: [ "pse", "tsc" ] }

Remove All Items Equaling Something Inside an Array of Objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Given the following documents in the ``tvShows`` collection:

.. code-block:: javascript

{ _id: 1, shows: [
{ name: "Spongebob", time: "8:00PM" },
{ name: "CatDog", time: "6:00AM" },
{ name: "Powerpuff Girls": time: "12:00PM" } ] }
{ _id: 2, shows: [
{ name: "Dexter's Lab", time: "6:00AM" },
{ name: "Power Rangers" time: "8:00PM" } ] }

The following operation removes any entry whose ``time`` property equals ``"8:00PM"`` in the ``shows`` array:

.. code-block:: javascript

db.tvShows.update(
{ "shows.time": "8:00PM" },
{ $pull: { shows: { time: "8:00PM" } } },
{ multi: true }
)

After the operation, no documents contain shows with an ``"8:00PM"`` ``time``:

.. code-block:: javascript

{ _id: 1, shows: [
{ name: "CatDog", time: "6:00AM" },
{ name: "Powerpuff Girls": time: "12:00PM" } ] }
{ _id: 2, shows: [
{ name: "Dexter's Lab", time: "6:00AM" } ] }


Remove All Items Greater Than a Specified Value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -70,3 +106,38 @@ After the update operation, the document only has values less than 6:
.. code-block:: javascript

{ _id: 1, votes: [ 3, 5 ] }


Remove All Items Greater Than a Specified Value Inside an Array of Objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Given the following documents in the ``tvShows`` collection:

.. code-block:: javascript

{ _id: 1, shows: [
{ name: "Spongebob", time: 8 },
{ name: "CatDog", time: 6 },
{ name: "Powerpuff Girls": time: 12 } ] }
{ _id: 2, shows: [
{ name: "Dexter's Lab", time: 6 },
{ name: "Power Rangers" time: 8 } ] }

The following operation removes any entry whose ``time`` property is greater than ``8``:

.. code-block:: javascript

db.tvShows.update(
{ "shows.time": "8:00PM" },
{ $pull: { shows: { time: { $gte: 8 } } } },
{ multi: true }
)

After the operation, no documents contain shows starting at or after 8:

.. code-block:: javascript

{ _id: 1, shows: [
{ name: "CatDog", time: 6 } ] }
{ _id: 2, shows: [
{ name: "Dexter's Lab", time: 6 } ] }