Skip to content

Commit 69b7136

Browse files
(DOCSP-9714): Perform CRUD Operations in a VSCE Playground (#10)
* (DOCSP-9714): create and update * (DOCSP-9714): read and delete * (DOCSP-9714): maybe fixing crud ops landing page * (DOCSP-9714): small tweaks * (DOCSP-9714): remove accidental file add from crud_ops toc * Copy tweaks Co-authored-by: jeff-allen-mongo <[email protected]>
1 parent 753f153 commit 69b7136

9 files changed

+665
-6
lines changed

snooty.toml

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

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

6-
toc_landing_pages = ["/playgrounds"]
6+
toc_landing_pages = ["/playgrounds", "/crud-ops"]
77

88
[constants]
99

source/create-document-playground.txt

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
.. _vsce-create-doc-playground:
2+
3+
================
4+
Create 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+
You can create documents in a collection using the
16+
:manual:`MongoDB CRUD Operators </crud>` in a MongoDB Playground:
17+
18+
- Use the :manual:`insertOne()
19+
</reference/method/db.collection.insertOne>` method to insert one
20+
document.
21+
- Use the :manual:`insertMany()
22+
</reference/method/db.collection.insertMany>` method to insert more
23+
than one document.
24+
25+
Prerequisites
26+
-------------
27+
28+
If you have not done so already, you must complete the following
29+
prerequisites before you can create documents with a MongoDB Playground:
30+
31+
- :ref:`Create a connection to a MongoDB deployment
32+
<vsce-connect-task>`.
33+
- :ref:`Activate the connection to the MongoDB deployment
34+
<vsce-activate-connection>`.
35+
- :ref:`Open a MongoDB Playground <open-playground-for-crud-vsce>`.
36+
37+
Create One Document
38+
-------------------
39+
40+
To create one document, use the following syntax in your Playground:
41+
42+
.. code-block:: javascript
43+
44+
db.collection.insertOne(
45+
<document>,
46+
{
47+
writeConcern: <document>
48+
}
49+
)
50+
51+
.. note::
52+
53+
If the database doesn't exist, insert operations will create it.
54+
55+
For a detailed description of this method's parameters,
56+
see :manual:`insertOne() </reference/method/db.collection.insertOne>`
57+
in the MongoDB Manual.
58+
59+
.. include:: /includes/run-playground.rst
60+
61+
Example
62+
~~~~~~~
63+
64+
.. include:: /includes/blank-playground.rst
65+
66+
The following example:
67+
68+
1. Switches to the ``test`` database.
69+
#. Inserts eight documents into the ``test.sales`` collection.
70+
71+
.. code-block:: javascript
72+
:copyable: false
73+
74+
use("test");
75+
76+
db.sales.insertOne(
77+
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z")}
78+
);
79+
80+
When you press the :guilabel:`Play Button`, this operation outputs the
81+
following document to the :guilabel:`Output` view in Visual Studio
82+
Code:
83+
84+
.. code-block:: javascript
85+
86+
{
87+
acknowleged: 1,
88+
insertedId: 1
89+
}
90+
91+
92+
Create Many Documents
93+
---------------------
94+
95+
To create many documents, use the following syntax in your Playground:
96+
97+
.. code-block:: javascript
98+
99+
db.collection.insertMany(
100+
[ <document 1> , <document 2>, ... ],
101+
{
102+
writeConcern: <document>,
103+
ordered: <boolean>
104+
}
105+
)
106+
107+
.. note::
108+
109+
If the database doesn't exist, insert operations will create it.
110+
111+
For a detailed description of this method's parameters,
112+
see :manual:`insertMany() </reference/method/db.collection.insertMany>`
113+
in the MongoDB Manual.
114+
115+
.. include:: /includes/run-playground.rst
116+
117+
Example
118+
~~~~~~~
119+
120+
.. include:: /includes/blank-playground.rst
121+
122+
The following example:
123+
124+
1. Switches to the ``test`` database.
125+
#. Inserts eight documents into the ``test.sales`` collection.
126+
127+
.. code-block:: javascript
128+
:copyable: false
129+
130+
use("test");
131+
132+
db.sales.insertMany([
133+
{ "_id" : 2, "item" : "abc", "price" : 10, "quantity" : 2, "date" : new Date("2014-03-01T08:00:00Z") },
134+
{ "_id" : 3, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : new Date("2014-03-01T09:00:00Z") },
135+
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : new Date("2014-03-15T09:00:00Z") },
136+
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : new Date("2014-04-04T11:21:39.736Z") },
137+
{ "_id" : 6, "item" : "abc", "price" : 10, "quantity" : 10, "date" : new Date("2014-04-04T21:23:13.331Z") },
138+
{ "_id" : 7, "item" : "def", "price" : 7.5, "quantity": 5, "date" : new Date("2015-06-04T05:08:13Z") },
139+
{ "_id" : 8, "item" : "def", "price" : 7.5, "quantity": 10, "date" : new Date("2015-09-10T08:43:00Z") },
140+
{ "_id" : 9, "item" : "abc", "price" : 10, "quantity" : 5, "date" : new Date("2016-02-06T20:20:13Z") },
141+
]);
142+
143+
When you press the :guilabel:`Play Button`, this operation outputs the
144+
following document to the :guilabel:`Output` view in Visual Studio
145+
Code:
146+
147+
.. code-block:: javascript
148+
:copyable: false
149+
150+
{
151+
acknowleged: 1,
152+
insertedIds: {
153+
'0': 2,
154+
'1': 3,
155+
'2': 4,
156+
'3': 5,
157+
'4': 6,
158+
'5': 7,
159+
'6': 8,
160+
'7': 9
161+
}
162+
}

source/crud-ops.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,41 @@ Perform CRUD Operations
1313
:class: singlecol
1414

1515
.. include:: /includes/fact-vsce-preview.rst
16+
17+
Documents are individual records in a MongoDB collection and are the
18+
basic unit of data in MongoDB.
19+
20+
You can use a MongoDB Playground to perform CRUD (create, read, update,
21+
and delete) operations on documents in a collection on a
22+
:doc:`connected deployment </connect>`.
23+
24+
Use the :manual:`MongoDB CRUD Operators </crud>` and
25+
:manual:`shell methods </reference/method>` to interact with your
26+
databases in MongoDB Playgrounds.
27+
28+
.. note::
29+
30+
You can also use your application to perform CRUD operations on
31+
documents using the appropriate :driver:`driver </>`. Playgrounds are
32+
meant to help you prototype database operations as you develop your
33+
application.
34+
35+
.. _open-playground-for-crud-vsce:
36+
37+
Open a Playground to Perform CRUD Operations
38+
--------------------------------------------
39+
40+
Before you can create, read, update, and delete documents using a
41+
MongoDB Playground, you must open one.
42+
43+
.. include:: /includes/steps/open-new-playground.rst
44+
45+
.. class:: hidden
46+
47+
.. toctree::
48+
:titlesonly:
49+
50+
/create-document-playground
51+
/read-document-playground
52+
/update-document-playground
53+
/delete-document-playground

source/delete-document-playground.txt

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
.. _vsce-delete-doc-playground:
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+
You delete documents in a collection using the
16+
:manual:`MongoDB CRUD Operators </crud>` in a MongoDB Playground:
17+
18+
- Use the :manual:`deleteOne()
19+
</reference/method/db.collection.deleteOne>` method to delete one
20+
document.
21+
- Use the :manual:`deleteMany()
22+
</reference/method/db.collection.deleteMany>` method to delete more
23+
than one document.
24+
25+
Prerequisites
26+
-------------
27+
28+
If you have not done so already, you must complete the following
29+
prerequisites before you can delete documents with a MongoDB Playground:
30+
31+
- :ref:`Create a connection to a MongoDB deployment
32+
<vsce-connect-task>`.
33+
- :ref:`Activate the connection to the MongoDB deployment
34+
<vsce-activate-connection>`.
35+
- :ref:`Open a MongoDB Playground <open-playground-for-crud-vsce>`.
36+
- :ref:`vsce-create-doc-playground` or create documents in a collection
37+
using a different method..
38+
39+
Delete One Document
40+
-------------------
41+
42+
To delete one document, use the following syntax in your Playground:
43+
44+
.. code-block:: javascript
45+
46+
db.collection.deleteOne(
47+
<filter>,
48+
{
49+
writeConcern: <document>,
50+
collation: <document>
51+
}
52+
)
53+
54+
For a detailed description of this method's parameters,
55+
see :manual:`deleteOne() </reference/method/db.collection.deleteOne>`
56+
in the MongoDB Manual.
57+
58+
.. include:: /includes/run-playground.rst
59+
60+
Example
61+
~~~~~~~
62+
63+
.. include:: /includes/blank-playground.rst
64+
65+
The following example:
66+
67+
1. Switches to the ``test`` database.
68+
#. Deletes one document in the ``test.sales`` collection that matches
69+
the query.
70+
71+
.. code-block:: javascript
72+
:copyable: false
73+
74+
use("test");
75+
76+
db.sales.deleteOne(
77+
{ "_id" : 1 }
78+
);
79+
80+
When you press the :guilabel:`Play Button`, this operation outputs the
81+
following document to the :guilabel:`Output` view in Visual Studio
82+
Code:
83+
84+
.. code-block:: javascript
85+
86+
{
87+
acknowleged: 1,
88+
deletedCount: 1
89+
}
90+
91+
Delete Many Documents
92+
---------------------
93+
94+
To delete many documents, use the following syntax in your Playground:
95+
96+
.. code-block:: javascript
97+
98+
db.collection.deleteMany(
99+
<filter>,
100+
{
101+
writeConcern: <document>,
102+
collation: <document>
103+
}
104+
)
105+
106+
For a detailed description of this method's parameters,
107+
see :manual:`deleteMany() </reference/method/db.collection.deleteMany>`
108+
in the MongoDB Manual.
109+
110+
.. include:: /includes/run-playground.rst
111+
112+
Example
113+
~~~~~~~
114+
115+
.. include:: /includes/blank-playground.rst
116+
117+
The following example:
118+
119+
1. Switches to the ``test`` database.
120+
#. Deletes all documents in the ``test.sales`` collection that match
121+
the query.
122+
123+
.. code-block:: javascript
124+
:copyable: false
125+
126+
use("test");
127+
128+
db.sales.deleteMany(
129+
{ "item" : "abc" }
130+
);
131+
132+
When you press the :guilabel:`Play Button`, this operation outputs the
133+
following document to the :guilabel:`Output` view in Visual Studio
134+
Code:
135+
136+
.. code-block:: javascript
137+
:copyable: false
138+
139+
{
140+
acknowleged: 1,
141+
deletedCount: 3
142+
}

source/includes/blank-playground.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
To use this example, **start with a blank MongoDB Playground** by
2+
clearing the template Playground if it is loaded.

source/includes/run-playground.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To run your Playground, press the :guilabel:`Play Button` at the top
2+
right of the Playground View. |vsce| outputs the results of your
3+
playground to the :guilabel:`Output` view in Visual Studio Code.

0 commit comments

Comments
 (0)