Skip to content

Commit df894f8

Browse files
author
Dave
authored
DOCSP-19939 update insert pt4 v5.1 (#91)
* DOCSP-19939 insert updates pt4 * Fix alignment * Update extended json
1 parent 8d1a8a3 commit df894f8

File tree

6 files changed

+120
-80
lines changed

6 files changed

+120
-80
lines changed

source/reference/method/db.collection.update.txt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ with :method:`~db.collection.update()`.
922922

923923
.. code-block:: javascript
924924

925-
db.books.insertMany([
925+
db.books.insertMany( [
926926
{
927927
_id: 5,
928928
item: "RQM909",
@@ -937,7 +937,7 @@ with :method:`~db.collection.update()`.
937937
info: { publisher: "1111", pages: 72 },
938938
reorder: true
939939
}
940-
])
940+
] )
941941

942942
The following operation specifies both the ``multi`` option and
943943
the ``upsert`` option. If matching documents exist, the
@@ -1071,10 +1071,10 @@ Create a ``members`` collection with the following documents:
10711071

10721072
.. code-block:: javascript
10731073

1074-
db.members.insertMany([
1074+
db.members.insertMany( [
10751075
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
10761076
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
1077-
])
1077+
] )
10781078

10791079
Assume that instead of separate ``misc1`` and ``misc2`` fields, you
10801080
want to gather these into a new ``comments`` field. The following
@@ -1137,11 +1137,11 @@ Create a ``students3`` collection with the following documents:
11371137

11381138
.. code-block:: javascript
11391139

1140-
db.students3.insert([
1140+
db.students3.insertMany( [
11411141
{ "_id" : 1, "tests" : [ 95, 92, 90 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
11421142
{ "_id" : 2, "tests" : [ 94, 88, 90 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
11431143
{ "_id" : 3, "tests" : [ 70, 75, 82 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
1144-
]);
1144+
] )
11451145

11461146
Using an aggregation pipeline, you can update the documents with the
11471147
calculated grade average and letter grade.
@@ -1223,11 +1223,11 @@ collection with the following documents:
12231223

12241224
.. code-block:: javascript
12251225

1226-
db.students.insertMany([
1226+
db.students.insertMany( [
12271227
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
12281228
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
12291229
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
1230-
])
1230+
] )
12311231

12321232
To update all elements that are greater than or equal to ``100`` in the
12331233
``grades`` array, use the filtered positional operator
@@ -1264,7 +1264,7 @@ collection with the following documents:
12641264

12651265
.. code-block:: javascript
12661266

1267-
db.students2.insertMany([
1267+
db.students2.insertMany( [
12681268
{
12691269
"_id" : 1,
12701270
"grades" : [
@@ -1281,7 +1281,7 @@ collection with the following documents:
12811281
{ "grade" : 85, "mean" : 85, "std" : 4 }
12821282
]
12831283
}
1284-
])
1284+
] )
12851285

12861286
To modify the value of the ``mean`` field for all elements in the
12871287
``grades`` array where the grade is greater than or equal to ``85``,
@@ -1332,14 +1332,14 @@ collection with the following documents:
13321332

13331333
.. code-block:: javascript
13341334

1335-
db.members.insertMany([
1335+
db.members.insertMany( [
13361336
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
13371337
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
13381338
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
13391339
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
13401340
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
13411341
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
1342-
])
1342+
] )
13431343

13441344
Create the following indexes on the collection:
13451345

@@ -1434,12 +1434,11 @@ In :binary:`~bin.mongosh`, create a collection named
14341434

14351435
.. code-block:: javascript
14361436

1437-
db.myColl.insertMany(
1438-
[
1437+
db.myColl.insertMany( [
14391438
{ _id: 1, category: "café", status: "A" },
14401439
{ _id: 2, category: "cafe", status: "a" },
14411440
{ _id: 3, category: "cafE", status: "a" }
1442-
])
1441+
] )
14431442

14441443
The following operation includes the :ref:`collation <collation>`
14451444
option and sets ``multi`` to ``true`` to update all matching documents:

source/reference/method/db.collection.updateMany.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ Create a ``members`` collection with the following documents:
413413

414414
.. code-block:: javascript
415415

416-
db.members.insertMany([
416+
db.members.insertMany( [
417417
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
418418
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
419-
])
419+
] )
420420

421421
Assume that instead of separate ``misc1`` and ``misc2`` fields, you
422422
want to gather these into a new ``comments`` field. The following
@@ -477,11 +477,11 @@ For example, create a ``students3`` collection with the following documents:
477477

478478
.. code-block:: javascript
479479

480-
db.students3.insert([
480+
db.students3.insertMany( [
481481
{ "_id" : 1, "tests" : [ 95, 92, 90 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
482482
{ "_id" : 2, "tests" : [ 94, 88, 90 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
483483
{ "_id" : 3, "tests" : [ 70, 75, 82 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
484-
]);
484+
] )
485485

486486
Using an aggregation pipeline, you can update the documents with the
487487
calculated grade average and letter grade.
@@ -682,11 +682,11 @@ Create a collection ``students`` with the following documents:
682682

683683
.. code-block:: javascript
684684

685-
db.students.insert([
685+
db.students.insertMany( [
686686
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
687687
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
688688
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
689-
])
689+
] )
690690

691691
To update all elements that are greater than or equal to ``100`` in the
692692
``grades`` array, use the filtered positional operator
@@ -716,7 +716,7 @@ Create a collection ``students2`` with the following documents:
716716

717717
.. code-block:: javascript
718718

719-
db.students2.insert([
719+
db.students2.insertMany( [
720720
{
721721
"_id" : 1,
722722
"grades" : [
@@ -733,7 +733,7 @@ Create a collection ``students2`` with the following documents:
733733
{ "grade" : 85, "mean" : 85, "std" : 4 }
734734
]
735735
}
736-
])
736+
] )
737737

738738

739739
To modify the value of the ``mean`` field for all elements in the
@@ -782,14 +782,14 @@ Create a sample ``members`` collection with the following documents:
782782

783783
.. code-block:: javascript
784784

785-
db.members.insertMany([
785+
db.members.insertMany( [
786786
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
787787
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
788788
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
789789
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
790790
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
791791
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
792-
])
792+
] )
793793

794794
Create the following indexes on the collection:
795795

source/reference/method/db.collection.updateOne.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,10 @@ Create a ``members`` collection with the following documents:
480480

481481
.. code-block:: javascript
482482

483-
db.members.insertMany([
483+
db.members.insertMany( [
484484
{ "_id" : 1, "member" : "abc123", "status" : "A", "points" : 2, "misc1" : "note to self: confirm status", "misc2" : "Need to activate", "lastUpdate" : ISODate("2019-01-01T00:00:00Z") },
485485
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, comments: [ "reminder: ping me at 100pts", "Some random comment" ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
486-
])
486+
] )
487487

488488
Assume that instead of separate ``misc1`` and ``misc2`` fields in the
489489
first document, you want to gather these into a ``comments`` field,
@@ -547,11 +547,11 @@ For example, create a ``students3`` collection with the following documents:
547547

548548
.. code-block:: javascript
549549

550-
db.students3.insert([
550+
db.students3.insertMany( [
551551
{ "_id" : 1, "tests" : [ 95, 92, 90 ], "average" : 92, "grade" : "A", "lastUpdate" : ISODate("2020-01-23T05:18:40.013Z") },
552552
{ "_id" : 2, "tests" : [ 94, 88, 90 ], "average" : 91, "grade" : "A", "lastUpdate" : ISODate("2020-01-23T05:18:40.013Z") },
553553
{ "_id" : 3, "tests" : [ 70, 75, 82 ], "lastUpdate" : ISODate("2019-01-01T00:00:00Z") }
554-
]);
554+
] )
555555

556556
The third document ``_id: 3`` is missing the ``average`` and ``grade``
557557
fields. Using an aggregation pipeline, you can update the document with
@@ -795,11 +795,11 @@ Create a collection ``students`` with the following documents:
795795

796796
.. code-block:: javascript
797797

798-
db.students.insert([
798+
db.students.insertMany( [
799799
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
800800
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
801801
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
802-
])
802+
] )
803803

804804
To modify all elements that are greater than or equal to ``100`` in the
805805
``grades`` array, use the filtered positional operator
@@ -831,7 +831,7 @@ Create a collection ``students2`` with the following documents:
831831

832832
.. code-block:: javascript
833833

834-
db.students2.insert([
834+
db.students2.insertMany( [
835835
{
836836
"_id" : 1,
837837
"grades" : [
@@ -848,7 +848,7 @@ Create a collection ``students2`` with the following documents:
848848
{ "grade" : 85, "mean" : 85, "std" : 4 }
849849
]
850850
}
851-
])
851+
] )
852852

853853
To modify the value of the ``mean`` field for all elements in the
854854
``grades`` array where the grade is greater than or equal to ``85``,
@@ -897,14 +897,14 @@ Create a sample ``members`` collection with the following documents:
897897

898898
.. code-block:: javascript
899899

900-
db.members.insertMany([
900+
db.members.insertMany( [
901901
{ "_id" : 1, "member" : "abc123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
902902
{ "_id" : 2, "member" : "xyz123", "status" : "A", "points" : 60, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" },
903903
{ "_id" : 3, "member" : "lmn123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
904904
{ "_id" : 4, "member" : "pqr123", "status" : "D", "points" : 20, "misc1" : "Deactivated", "misc2" : null },
905905
{ "_id" : 5, "member" : "ijk123", "status" : "P", "points" : 0, "misc1" : null, "misc2" : null },
906906
{ "_id" : 6, "member" : "cde123", "status" : "A", "points" : 86, "misc1" : "reminder: ping me at 100pts", "misc2" : "Some random comment" }
907-
])
907+
] )
908908

909909
Create the following indexes on the collection:
910910

source/reference/method/db.createCollection.txt

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,6 @@ for the past 24 hours, issue this command:
466466
Create a Collection with Document Validation
467467
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
468468

469-
.. versionadded:: 3.2
470-
471469
Collections with validation compare each inserted or updated document
472470
against the criteria specified in the ``validator`` option. Depending
473471
on the ``validationLevel`` and ``validationAction``, MongoDB either
@@ -477,11 +475,6 @@ fails to meet the specified criteria.
477475
The following example creates a ``contacts`` collection with a JSON
478476
Schema validator:
479477

480-
.. note::
481-
482-
MongoDB 3.6 adds the :query:`$jsonSchema` operator to support JSON
483-
Schema validation.
484-
485478
.. code-block:: json
486479

487480
db.createCollection( "contacts", {
@@ -510,23 +503,45 @@ With the validator in place, the following insert operation fails validation:
510503

511504
.. code-block:: javascript
512505

513-
db.contacts.insert( { name: "Amanda", status: "Updated" } )
506+
db.contacts.insertOne( { name: "Amanda", status: "Updated" } )
514507

515-
The method returns the error in the ``WriteResult``:
508+
The method returns the error:
516509

517510
.. code-block:: javascript
518511

519-
WriteResult({
520-
"nInserted" : 0,
521-
"writeError" : {
522-
"code" : 121,
523-
"errmsg" : "Document failed validation"
524-
}
525-
})
526-
527-
For more information, see :doc:`/core/schema-validation`. To view the
528-
validation specifications for a collection, use the
529-
:method:`db.getCollectionInfos()` method.
512+
Uncaught:
513+
MongoServerError: Document failed validation
514+
Additional information: {
515+
failingDocumentId: ObjectId("61a8f4847a818411619e952e"),
516+
details: {
517+
operatorName: '$jsonSchema',
518+
schemaRulesNotSatisfied: [
519+
{
520+
operatorName: 'properties',
521+
propertiesNotSatisfied: [
522+
{
523+
propertyName: 'status',
524+
description: 'can only be one of the enum values',
525+
details: [ [Object] ]
526+
}
527+
]
528+
},
529+
{
530+
operatorName: 'required',
531+
specifiedAs: { required: [ 'phone' ] },
532+
missingProperties: [ 'phone' ]
533+
}
534+
]
535+
}
536+
}
537+
538+
To view the validation specifications for a collection, use
539+
:method:`db.getCollectionInfos()`.
540+
541+
.. seealso::
542+
543+
- :doc:`/core/schema-validation`
544+
- :query:`$jsonSchema`
530545

531546
.. _createCollection-collation-example:
532547

source/reference/method/js-collection.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ Collection Methods
132132

133133
- Hides an index from the query planner.
134134

135-
* - :method:`db.collection.insert()`
136-
137-
- Creates a new document in a collection.
138-
139135
* - :method:`db.collection.insertOne()`
140136

141137
- Inserts a new document in a collection.

0 commit comments

Comments
 (0)