@@ -90,17 +90,15 @@ The following example writes the sample BSON document to ``file.bson``:
90
90
91
91
.. code-block:: javascript
92
92
93
- const fs = require( 'fs') ; // Import the file system module
94
- const BSON = require( 'bson') ; // Import the BSON package
93
+ import fs from 'fs/promises' ; // Import the file system module
94
+ import { BSON } from 'bson'; // Import the BSON package
95
95
96
96
// Create a BSON object
97
97
const bsonData = BSON.serialize(result);
98
98
99
99
// Write the BSON data to a file
100
- fs.writeFile('file.bson', bsonData, (err) => {
101
- if (err) throw err;
102
- console.log('BSON data written to file.bson');
103
- });
100
+ await fs.writeFile('file.bson', bsonData);
101
+ console.log('BSON data written to file.bson');
104
102
105
103
Read BSON from a File
106
104
---------------------
@@ -116,25 +114,23 @@ The following example reads the sample BSON document from ``file.bson``:
116
114
.. input::
117
115
:language: javascript
118
116
119
- const fs = require( 'fs') ; // Import the file system module
120
- const BSON = require( 'bson') ; // Import the BSON package
117
+ import fs from 'fs/promises' ; // Import the file system module
118
+ import { BSON } from 'bson'; // Import the BSON package
121
119
122
120
// Read the BSON data from a file
123
- fs.readFile('file.bson', (err, data) => {
124
- if (err) throw err;
125
- const document = BSON.deserialize(data);
126
- console.log(document);
127
- });
121
+ const data = await fs.readFile('file.bson');
122
+ const document = BSON.deserialize(data);
123
+ console.log(document);
128
124
129
125
.. output::
130
126
:visible: false
131
127
132
128
{
133
- _id: new ObjectId('67e1823d0d63bfdf87e8928e'),
134
- " address" : {" street": " Pizza St", " zipcode": " 10003"},
135
- " coord" : [-73.982419, 41.579505],
136
- " cuisine": " Pizza",
137
- " name" : "Mongo's Pizza"
129
+ _id: new ObjectId('67e1823d0d63bfdf87e8928e'),
130
+ address: { street: ' Pizza St', zipcode: ' 10003' },
131
+ coord: [ -73.982419, 41.579505 ],
132
+ cuisine: ' Pizza',
133
+ name: "Mongo's Pizza"
138
134
}
139
135
140
136
API Documentation
0 commit comments