Skip to content

Commit af21615

Browse files
author
Dave
authored
DOCSP-19939 update insert pt4 v5.1 (#91) (#97)
* DOCSP-19939 insert updates pt4 * Fix alignment * Update extended json
1 parent 385b3cf commit af21615

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
@@ -918,7 +918,7 @@ with :method:`~db.collection.update()`.
918918

919919
.. code-block:: javascript
920920

921-
db.books.insertMany([
921+
db.books.insertMany( [
922922
{
923923
_id: 5,
924924
item: "RQM909",
@@ -933,7 +933,7 @@ with :method:`~db.collection.update()`.
933933
info: { publisher: "1111", pages: 72 },
934934
reorder: true
935935
}
936-
])
936+
] )
937937

938938
The following operation specifies both the ``multi`` option and
939939
the ``upsert`` option. If matching documents exist, the
@@ -1067,10 +1067,10 @@ Create a ``members`` collection with the following documents:
10671067

10681068
.. code-block:: javascript
10691069

1070-
db.members.insertMany([
1070+
db.members.insertMany( [
10711071
{ "_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") },
10721072
{ "_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") }
1073-
])
1073+
] )
10741074

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

11341134
.. code-block:: javascript
11351135

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

11421142
Using an aggregation pipeline, you can update the documents with the
11431143
calculated grade average and letter grade.
@@ -1219,11 +1219,11 @@ collection with the following documents:
12191219

12201220
.. code-block:: javascript
12211221

1222-
db.students.insertMany([
1222+
db.students.insertMany( [
12231223
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
12241224
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
12251225
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
1226-
])
1226+
] )
12271227

12281228
To update all elements that are greater than or equal to ``100`` in the
12291229
``grades`` array, use the filtered positional operator
@@ -1260,7 +1260,7 @@ collection with the following documents:
12601260

12611261
.. code-block:: javascript
12621262

1263-
db.students2.insertMany([
1263+
db.students2.insertMany( [
12641264
{
12651265
"_id" : 1,
12661266
"grades" : [
@@ -1277,7 +1277,7 @@ collection with the following documents:
12771277
{ "grade" : 85, "mean" : 85, "std" : 4 }
12781278
]
12791279
}
1280-
])
1280+
] )
12811281

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

13291329
.. code-block:: javascript
13301330

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

13401340
Create the following indexes on the collection:
13411341

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

14311431
.. code-block:: javascript
14321432

1433-
db.myColl.insertMany(
1434-
[
1433+
db.myColl.insertMany( [
14351434
{ _id: 1, category: "café", status: "A" },
14361435
{ _id: 2, category: "cafe", status: "a" },
14371436
{ _id: 3, category: "cafE", status: "a" }
1438-
])
1437+
] )
14391438

14401439
The following operation includes the :ref:`collation <collation>`
14411440
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
@@ -130,10 +130,6 @@ Collection Methods
130130

131131
- Hides an index from the query planner.
132132

133-
* - :method:`db.collection.insert()`
134-
135-
- Creates a new document in a collection.
136-
137133
* - :method:`db.collection.insertOne()`
138134

139135
- Inserts a new document in a collection.

0 commit comments

Comments
 (0)