Skip to content

Commit 7601e88

Browse files
(DOCSP-10504): CRUD ops for mongosh (#9)
* (DOCSP-10504): CRUD ops for mongosh * Apply suggestions from code review Co-authored-by: jwilliams-mongo <[email protected]> * updates per copy review * updates per copy review rd 2 Co-authored-by: jwilliams-mongo <[email protected]>
1 parent 0150a42 commit 7601e88

File tree

9 files changed

+668
-1
lines changed

9 files changed

+668
-1
lines changed

snooty.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title = "MongoDB Shell"
33

44
intersphinx = ["https://docs.mongodb.com/manual/objects.inv"]
55

6-
toc_landing_pages = []
6+
toc_landing_pages = ["/crud"]
77

88
[constants]
99
mdb-shell = "The MongoDB Shell"

source/crud.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
11
=======================
22
Perform CRUD Operations
33
=======================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
.. include:: /includes/admonitions/fact-mdb-shell-beta.rst
14+
15+
CRUD operations *create*, *read*, *update*, and *delete* documents.
16+
17+
Create Operations
18+
-----------------
19+
20+
Create or insert operations add new documents to a collection. If the
21+
collection does not exist, create operations also create the
22+
collection.
23+
24+
You can insert a single document or multiple documents in a single
25+
operation.
26+
27+
For examples, see :ref:`mongosh-insert`.
28+
29+
Read Operations
30+
---------------
31+
32+
Read operations retrieve documents from a collection; i.e. query a
33+
collection for documents.
34+
35+
You can specify criteria, or filters, that identify the documents to
36+
return.
37+
38+
For examples, see :ref:`mongosh-read`.
39+
40+
Update Operations
41+
-----------------
42+
43+
Update operations modify existing documents in a collection. You can
44+
update a single document or multiple documents in a single operation.
45+
46+
You can specify criteria, or filters, that identify the documents to
47+
update. These filters use the same syntax as read operations.
48+
49+
For examples, see :ref:`mongosh-update`.
50+
51+
Delete Operations
52+
-----------------
53+
54+
Delete operations remove existing documents from a collection. You can
55+
remove a single document or multiple documents in a single operation.
56+
57+
You can specify criteria, or filters, that identify the documents to
58+
remove. These filters use the same syntax as read operations.
59+
60+
For examples, see :ref:`mongosh-delete`.
61+
62+
.. class:: hidden
63+
64+
.. toctree::
65+
:titlesonly:
66+
67+
/crud/insert
68+
/crud/read
69+
/crud/update
70+
/crud/delete

source/crud/delete.txt

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
.. _mongosh-delete:
2+
3+
================
4+
Delete Documents
5+
================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
.. include:: /includes/admonitions/fact-mdb-shell-beta.rst
16+
17+
The MongoDB shell provides the following methods to delete documents
18+
from a collection:
19+
20+
- To delete multiple documents, use
21+
:method:`db.collection.deleteMany()`.
22+
23+
- To delete a single document, use :method:`db.collection.deleteOne()`.
24+
25+
.. include:: /includes/fact-sample-data-examples.rst
26+
27+
Delete All Documents
28+
--------------------
29+
30+
To delete all documents from a collection, pass an empty
31+
:ref:`filter<document-query-filter>` document ``{}`` to the
32+
:method:`db.collection.deleteMany()` method.
33+
34+
.. example::
35+
36+
To delete all documents from the ``sample_mflix.movies`` collection:
37+
38+
.. code-block:: javascript
39+
40+
use sample_mflix
41+
42+
db.movies.deleteMany({})
43+
44+
The method returns a document with the status of the operation. For
45+
more information and examples, see
46+
:method:`~db.collection.deleteMany()`.
47+
48+
Delete All Documents that Match a Condition
49+
-------------------------------------------
50+
51+
You can specify criteria, or filters, that identify the documents to
52+
delete. The :ref:`filters <document-query-filter>` use the same syntax
53+
as read operations.
54+
55+
To specify equality conditions, use ``<field>:<value>`` expressions in
56+
the query filter document.
57+
58+
To delete all documents that match a deletion criteria, pass a filter
59+
parameter to the :method:`~db.collection.deleteMany()` method.
60+
61+
.. example::
62+
63+
To delete all documents from the ``sample_mflix.movies`` collection
64+
where the ``title`` equals ``"Titanic"``:
65+
66+
.. code-block:: javascript
67+
68+
use sample_mflix
69+
70+
db.movies.deleteMany( { title: "Titanic" } )
71+
72+
The method returns a document with the status of the operation. For
73+
more information and examples, see
74+
:method:`~db.collection.deleteMany()`.
75+
76+
Delete Only One Document that Matches a Condition
77+
-------------------------------------------------
78+
79+
To delete at most a single document that matches a specified filter
80+
(even though multiple documents may match the specified filter) use the
81+
:method:`db.collection.deleteOne()` method.
82+
83+
.. example::
84+
85+
To delete the *first* document from the ``sample_mflix.movies``
86+
collection where the ``cast`` array contains ``"Brad Pitt"``:
87+
88+
.. code-block:: javascript
89+
90+
use sample_mflix
91+
92+
db.movies.deleteOne( { cast: "Brad Pitt" } )
93+
94+
.. include:: /includes/admonitions/note-natural-sort-order.rst
95+
96+
Delete Behavior
97+
---------------
98+
99+
To learn more about the specific behavior of deleting documents,
100+
see :manual:`Behavior </tutorial/remove-documents/#delete-behavior>`.
101+
102+
Learn More
103+
----------
104+
105+
- To see additional examples of deleting documents, see the following
106+
method pages:
107+
108+
- :method:`db.collection.deleteMany()`
109+
- :method:`db.collection.deleteOne()`
110+
111+
- To see all available methods to delete documents, see
112+
:manual:`Delete Methods </reference/delete-methods/>`.

source/crud/insert.txt

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
.. _mongosh-insert:
2+
3+
================
4+
Insert Documents
5+
================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
.. include:: /includes/admonitions/fact-mdb-shell-beta.rst
16+
17+
The MongoDB shell provides the following methods to insert documents
18+
into a collection:
19+
20+
- To insert a single document, use :method:`db.collection.insertOne()`.
21+
22+
- To insert multiple documents, use
23+
:method:`db.collection.insertMany()`.
24+
25+
.. include:: /includes/fact-sample-data-examples.rst
26+
27+
Insert a Single Document
28+
------------------------
29+
30+
:method:`db.collection.insertOne()` inserts a *single*
31+
:ref:`document<bson-document-format>` into a collection. If the document
32+
does not specify an ``_id`` field, MongoDB adds the ``_id`` field with
33+
an ObjectId value to the new document. See
34+
:ref:`write-op-insert-behavior`.
35+
36+
.. example::
37+
38+
To insert a new document into the ``sample_mflix.movies`` collection:
39+
40+
.. code-block:: javascript
41+
42+
use sample_mflix
43+
44+
db.movies.insertOne(
45+
{
46+
title: "The Favourite",
47+
genres: [ "Drama", History" ]
48+
runtime: 121,
49+
rated: "R",
50+
year: 2018,
51+
directors: [ "Yorgos Lanthimos" ],
52+
cast: [ "Olivia Colman", "Emma Stone", "Rachel Weisz" ],
53+
type: "movie"
54+
}
55+
)
56+
57+
:method:`~db.collection.insertOne()` returns a document that
58+
includes the newly inserted document's ``_id`` field value.
59+
60+
To retrieve the inserted document,
61+
:ref:`read the collection <mongosh-read>`:
62+
63+
.. code-block:: javascript
64+
65+
db.inventory.find( { title: "The Favourite" } )
66+
67+
.. note::
68+
69+
To ensure you return the document you inserted, you can instead
70+
query by ``_id``.
71+
72+
Insert Multiple Documents
73+
-------------------------
74+
75+
:method:`db.collection.insertMany()` can insert *multiple*
76+
:ref:`documents <bson-document-format>` into a collection. Pass an array
77+
of documents to the method. If the documents do not specify an ``_id``
78+
field, MongoDB adds the ``_id`` field with an ObjectId value to each
79+
document. See :ref:`write-op-insert-behavior`.
80+
81+
.. example::
82+
83+
To insert two new documents into the ``sample_mflix.movies``
84+
collection:
85+
86+
.. code-block:: javascript
87+
88+
use sample_mflix
89+
90+
db.movies.insertMany([
91+
{
92+
title: "Jurassic World: Fallen Kingdom",
93+
genres: [ "Action", "Sci-Fi" ],
94+
runtime: 130,
95+
rated: "PG-13",
96+
year: 2018,
97+
directors: [ "J. A. Bayona" ],
98+
cast: [ "Chris Pratt", "Bryce Dallas Howard", "Rafe Spall" ],
99+
type: "movie"
100+
},
101+
{
102+
title: "Tag",
103+
genres: [ "Comedy", "Action" ],
104+
runtime: 105,
105+
rated: "R",
106+
year: 2018,
107+
directors: [ "Jeff Tomsic" ],
108+
cast: [ "Annabelle Wallis", "Jeremy Renner", "Jon Hamm" ],
109+
type: "movie"
110+
}
111+
])
112+
113+
:method:`~db.collection.insertMany()` returns a document that
114+
includes the newly inserted documents' ``_id`` field values.
115+
116+
To :ref:`read documents in the collection <mongosh-read>`:
117+
118+
.. code-block:: javascript
119+
120+
db.movies.find( {} )
121+
122+
Insert Behavior
123+
---------------
124+
125+
To learn more about the specific behavior of inserting documents,
126+
see :ref:`write-op-insert-behavior`.
127+
128+
Learn More
129+
----------
130+
131+
- To see more examples of inserting documents into a collection, see
132+
the :method:`~db.collection.insertOne()` and
133+
:method:`db.collection.insertMany()` method pages.
134+
135+
- To see all available methods to insert documents into a collection,
136+
see :manual:`Additional Methods for Inserts
137+
</reference/insert-methods/#additional-inserts>`

0 commit comments

Comments
 (0)