@@ -49,6 +49,8 @@ The ``EJSON.stringify()`` method has these fields:
49
49
- Modifies output. If the value exists but it is not an array or a
50
50
function, ``EJSON.stringify()`` returns all document fields.
51
51
52
+ ``replacer`` can be an array or a function.
53
+
52
54
.. list-table::
53
55
:header-rows: 1
54
56
:widths: 30 70
@@ -57,13 +59,19 @@ The ``EJSON.stringify()`` method has these fields:
57
59
- Effect
58
60
59
61
* - array
60
- - An array of document fields to include in the output
62
+ - An array of document fields to include in the output. The
63
+ array elements must specify the field names to include in
64
+ the returned JSON string.
61
65
62
66
* - function
63
67
- A function that takes two parameters, ``key`` and
64
68
``value``. ``key`` provides the function's ``this``
65
- context. EJSON returns the transformed ``value``. See
66
- below for an :ref:`example <ex-ejson-function>`
69
+ context. EJSON returns the transformed ``value``.
70
+
71
+ The function is run for each object. The object
72
+ values are replaced with the function's return value.
73
+
74
+ For an example, see :ref:`ex-ejson-function`.
67
75
68
76
* - ``spacer``
69
77
- integer or string
@@ -209,7 +217,7 @@ Use a Function to Transform Output Fields
209
217
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
210
218
211
219
To transform field values, use a JavaScript function to set the
212
- ``replace `` option.
220
+ ``replacer `` option. For example:
213
221
214
222
.. code-block:: javascript
215
223
@@ -229,7 +237,10 @@ To transform field values, use a JavaScript function to set the
229
237
230
238
The function runs recursively against the input object.
231
239
240
+ Example output:
241
+
232
242
.. code-block:: javascript
243
+ :copyable: false
233
244
234
245
[
235
246
{
@@ -273,6 +284,156 @@ The function also modifies the ``quantity`` field in the output string.
273
284
All of the ``quantity`` values are multiplied by two in the output
274
285
string. ``EJSON.stringify()`` does not update the collection.
275
286
287
+ Use a Function to Transform Output Fields in Nested Objects
288
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289
+
290
+ Create the ``salesWithAddress`` collection with nested addresses:
291
+
292
+ .. code-block:: javascript
293
+
294
+ db.salesWithAddress.insertMany( [
295
+ { custId: 345, purchaseDate: ISODate("2023-07-04"),
296
+ quantity: 4, cost: Decimal128("100.60"),
297
+ address: { number: 100, street: "Main Street", ZIP: 12345 } },
298
+ { custId: 346, purchaseDate: ISODate("2023-07-12"),
299
+ quantity: 3, cost: Decimal128("175.45"),
300
+ address: { number: 200, street: "East Street", ZIP: 12345 } }
301
+ ] )
302
+
303
+ The following example uses a ``replacer`` function to change the ZIP
304
+ codes for the addresses to ``55555``:
305
+
306
+ .. code-block:: javascript
307
+
308
+ // Retrieve the salesWithAddress contents as an array and save
309
+ // in queryResults
310
+ let queryResults = db.salesWithAddress.find().toArray()
311
+
312
+ // Define a replacer function to change the ZIP codes
313
+ let replacer = function( key, value ) {
314
+ if (key === 'address') {
315
+ value.ZIP = 55555;
316
+ }
317
+ return value;
318
+ }
319
+
320
+ // Run EJSON.stringify() to change the ZIP codes in queryResults
321
+ EJSON.stringify( queryResults, replacer, 3 )
322
+
323
+ Example output:
324
+
325
+ .. code-block:: javascript
326
+ :copyable: false
327
+
328
+ [
329
+ {
330
+ "_id": {
331
+ "$oid": "65498c6562f443aa1490070f"
332
+ },
333
+ "custId": 345,
334
+ "purchaseDate": {
335
+ "$date": "2023-07-04T00:00:00Z"
336
+ },
337
+ "quantity": 4,
338
+ "cost": {
339
+ "$numberDecimal": "100.60"
340
+ },
341
+ "address": {
342
+ "number": 100,
343
+ "street": "Main Street",
344
+ "ZIP": 55555
345
+ }
346
+ },
347
+ {
348
+ "_id": {
349
+ "$oid": "65498c6562f443aa14900710"
350
+ },
351
+ "custId": 346,
352
+ "purchaseDate": {
353
+ "$date": "2023-07-12T00:00:00Z"
354
+ },
355
+ "quantity": 3,
356
+ "cost": {
357
+ "$numberDecimal": "175.45"
358
+ },
359
+ "address": {
360
+ "number": 200,
361
+ "street": "East Street",
362
+ "ZIP": 55555
363
+ }
364
+ }
365
+ ]
366
+
367
+ Use a Function to Replace BSON Strings
368
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369
+
370
+ For a list of BSON data types and the corresponding numeric codes, see
371
+ :manual:`BSON Types </reference/bson-types>`.
372
+
373
+ The following example uses a ``replacer`` function to replace the BSON
374
+ strings with the string ``"This is a string"``:
375
+
376
+ .. code-block:: javascript
377
+
378
+ // Retrieve the salesWithAddress contents as an array and save
379
+ // in queryResults
380
+ let queryResults = db.salesWithAddress.find().toArray()
381
+
382
+ // Define a replacer function to replace the strings
383
+ let replacer = function( key, value ) {
384
+ if (typeof value === "string") {
385
+ return "This is a string";
386
+ }
387
+ return value;
388
+ }
389
+
390
+ // Run EJSON.stringify() to replace the strings in queryResults
391
+ EJSON.stringify( queryResults, replacer, 3 )
392
+
393
+ Example output:
394
+
395
+ .. code-block:: javascript
396
+ :copyable: false
397
+
398
+ [
399
+ {
400
+ "_id": {
401
+ "$oid": "This is a string"
402
+ },
403
+ "custId": 345,
404
+ "purchaseDate": {
405
+ "$date": "This is a string"
406
+ },
407
+ "quantity": 4,
408
+ "cost": {
409
+ "$numberDecimal": "This is a string"
410
+ },
411
+ "address": {
412
+ "number": 100,
413
+ "street": "This is a string",
414
+ "ZIP": 12345
415
+ }
416
+ },
417
+ {
418
+ "_id": {
419
+ "$oid": "This is a string"
420
+ },
421
+ "custId": 346,
422
+ "purchaseDate": {
423
+ "$date": "This is a string"
424
+ },
425
+ "quantity": 3,
426
+ "cost": {
427
+ "$numberDecimal": "This is a string"
428
+ },
429
+ "address": {
430
+ "number": 200,
431
+ "street": "This is a string",
432
+ "ZIP": 12345
433
+ }
434
+ }
435
+ ]
436
+
276
437
Write to a File from Inside mongosh
277
438
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278
439
0 commit comments