You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the collection specified by the `$out` operation already exists, then the `$out` stage atomically replaces the existing collection with the new results collection upon completion of the aggregation.
95
95
96
-
To avoid overwriting the existing collection we can use `$merge` instead of `$out`.
96
+
To avoid overwriting the existing collection, we can use `$merge` instead of `$out`.
97
97
98
98
```
99
99
{ $merge : { into : "newCollection" } }
100
100
```
101
101
102
-
-if the collection does not exists, it will be created
103
-
-if it exists, new data will be added
104
-
-if[a doc already exists](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/#std-label-merge-whenMatched), we can replace it, keep the existing one, merge both documents cause the stage to fail or run a pipeline.
102
+
-If the collection does not exist, it will be created.
103
+
-If it exists, new data will be added.
104
+
-If[a doc already exists](https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/#std-label-merge-whenMatched), we can replace it, keep the existing one, merge both documents, and cause the stage to fail or run a pipeline.
105
105
106
-
This is perfect for creating [On-Demand Materialized Views](https://www.mongodb.com/docs/manual/core/materialized-views/)
106
+
This is perfect for creating [on-demand materialized views](https://www.mongodb.com/docs/manual/core/materialized-views/)
107
107
108
-
As an example, let's say we want the authors to contain all the books they've written, with all the book information. In this case, we'll do a `$lookup` to get the book information into the authors collection. We can even use the name `books` for the resulting data we're joining, shadowing the original `books` array we have in authors. This way it will look like the `books` array changes.
108
+
As an example, let's say we want the authors to contain all the books they've written, with all the book information. In this case, we'll do a `$lookup` to get the book information into the authors collection. We can even use the name `books` for the resulting data we're joining, shadowing the original `books` array we have in authors. This way, it will look like the `books` array changes.
109
109
110
110
```js
111
111
[
@@ -119,7 +119,7 @@ As an example, let's say we want the authors to contain all the books they've wr
119
119
]
120
120
```
121
121
122
-
Now a book will look like this. You can see that the books array has been "overwritten" by the `$lookup`.
122
+
Now, a book will look like this. You can see that the books array has been "overwritten" by the `$lookup`.
123
123
124
124
```js
125
125
{
@@ -249,7 +249,7 @@ We can go ahead and remove the authors from the books array, as it is redundant:
249
249
]
250
250
```
251
251
252
-
Now that our authors look the way we want, we can overwrite the authors collection using `$merge`
252
+
Now that our authors look the way we want, we can overwrite the authors collection using `$merge`.
253
253
254
254
```js
255
255
[
@@ -269,9 +269,9 @@ Now that our authors look the way we want, we can overwrite the authors collecti
269
269
]
270
270
```
271
271
272
-
-we use the `_id` field to match documents
273
-
-we replace the existing ones with `replace`
272
+
-We use the `_id` field to match documents.
273
+
-We replace the existing ones with `replace`.
274
274
275
275
:::warning
276
276
We should see a message telling us that the $merge operator will cause the pipeline to persist the results to the specified location. This stage changes data.
0 commit comments