|
| 1 | +const { Buffer } = require('node:buffer'); |
| 2 | + |
| 3 | +use('test'); |
| 4 | + |
| 5 | +// Create a document with all the BSON types. |
| 6 | +const doc = { |
| 7 | + double: new Double(1.2), // Double, 1, double |
| 8 | + string: 'Hello, world!', // String, 2, string |
| 9 | + object: { key: 'value' }, // Object, 3, object |
| 10 | + array: [1, 2, 3], // Array, 4, array |
| 11 | + binData: new Binary(Buffer.from([1, 2, 3])), // Binary data, 5, binData |
| 12 | + // Undefined, 6, undefined (deprecated) |
| 13 | + objectId: new ObjectId(), // ObjectId, 7, objectId |
| 14 | + boolean: true, // Boolean, 8, boolean |
| 15 | + date: new Date(), // Date, 9, date |
| 16 | + null: null, // Null, 10, null |
| 17 | + regex: new BSONRegExp('pattern', 'i'), // Regular Expression, 11, regex |
| 18 | + // DBPointer, 12, dbPointer (deprecated) |
| 19 | + javascript: new Code('function() {}'), // JavaScript, 13, javascript |
| 20 | + symbol: new BSONSymbol('symbol'), // Symbol, 14, symbol (deprecated) |
| 21 | + javascriptWithScope: new Code('function() {}', { foo: 1, bar: 'a' }), // JavaScript code with scope 15 "javascriptWithScope" Deprecated in MongoDB 4.4. |
| 22 | + int: new Int32(12345), // 32-bit integer, 16, "int" |
| 23 | + timestamp: new Timestamp(), // Timestamp, 17, timestamp |
| 24 | + long: new Long('123456789123456789'), // 64-bit integer, 18, long |
| 25 | + decimal: new Decimal128(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])), // Decimal128, 19, decimal |
| 26 | + minKey: new MinKey(), // Min key, -1, minKey |
| 27 | + maxKey: new MaxKey(), // Max key, 127, maxKey |
| 28 | + |
| 29 | + binDataSubtypes: { |
| 30 | + generic: new Binary(Buffer.from([1, 2, 3]), 0), // 0 |
| 31 | + functionData: new Binary('//8=', 1), // 1 |
| 32 | + binaryOld: new Binary('//8=', 2), // 2 |
| 33 | + uuidOld: new Binary('c//SZESzTGmQ6OfR38A11A==', 3), // 3 |
| 34 | + uuid: new UUID('AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA'), // 4 |
| 35 | + md5: new Binary('c//SZESzTGmQ6OfR38A11A==', 5), // 5 |
| 36 | + encrypted: new Binary('c//SZESzTGmQ6OfR38A11A==', 6), // 6 |
| 37 | + compressedTimeSeries: new Binary('c//SZESzTGmQ6OfR38A11A==', 0), // 7 |
| 38 | + custom: new Binary('//8=', 128), // 128 |
| 39 | + }, |
| 40 | + |
| 41 | + dbRef: new DBRef('namespace', new ObjectId()), // not actually a separate type, just a convention |
| 42 | +}; |
| 43 | + |
| 44 | +// Insert the document into the `all_types` collection. |
| 45 | +db.all_types.insertOne(doc); |
0 commit comments