How to use the Binary function from mongodb

Find comprehensive JavaScript mongodb.Binary code examples handpicked from public code repositorys.

The mongodb.Binary class is used to create a binary data representation of any data type supported by Node.js's Buffer class, to be stored in MongoDB.

24
25
26
27
28
29
30
31
32
33

  return db
}

// expose bson stuff visible in the shell
module.exports.Binary = mongodb.Binary
module.exports.Code = mongodb.Code
module.exports.DBRef = mongodb.DBRef
module.exports.Double = mongodb.Double
module.exports.Int32 = mongodb.Int32
fork icon272
star icon0
watch icon59

+ 15 other calls in file

52
53
54
55
56
57
58
59
60
61
  );
});

it('should convert uuid from mongo BSON binary', () => {
  const mUUID = MUUID.from(
    new Binary(uuidv1(null, Buffer.alloc(16)), Binary.SUBTYPE_UUID)
  );
  assert.equal(mUUID instanceof Binary, true);
  assert.equal(validate(mUUID.toString()), true);
});
fork icon15
star icon97
watch icon0

+ 2 other calls in file

How does mongodb.Binary work?

mongodb.Binary is a class provided by the MongoDB Node.js driver that represents binary data in a BSON format, and it can be used to store and retrieve binary data in MongoDB. It takes two arguments: a buffer containing the binary data and a sub-type that specifies how the data is encoded.

1299
1300
1301
1302
1303
1304
1305
1306
1307
  const tile = await tiles.geojson2pbf(geo.result2geojson(esResponse), xyz)
  // 204 = no-content, better than 404
  if (!tile) return res.status(204).send()
  res.type('application/x-protobuf')
  // write in cache without await on purpose for minimal latency, a cache failure must be detected in the logs
  if (!config.cache.disabled) cache.set(db, cacheHash, new mongodb.Binary(tile))
  res.throttleEnd()
  return res.status(200).send(tile)
}
fork icon6
star icon26
watch icon4

+ 20 other calls in file

1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
isValidDatabaseObject(object) {
  return object instanceof mongodb.Binary || this.isBase64Value(object);
},

JSONToDatabase(json) {
  return new mongodb.Binary(Buffer.from(json.base64, 'base64'));
},

isValidJSON(value) {
  return (
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const { MongoClient } = require("mongodb");
const fs = require("fs");
const mongodb = require("mongodb");
const binary = mongodb.Binary;

const uri = "mongodb://localhost:27017";
const client = new MongoClient(uri, { useUnifiedTopology: true });

async function run() {
  try {
    await client.connect();
    console.log("Connected to MongoDB");

    const database = client.db("mydb");
    const collection = database.collection("mycollection");

    // Read binary data from a file
    const data = fs.readFileSync("/path/to/file.pdf");

    // Insert binary data into MongoDB
    await collection.insertOne({ file: binary(data) });
    console.log("Inserted binary data into MongoDB");
  } catch (err) {
    console.error(err);
  } finally {
    await client.close();
    console.log("Connection to MongoDB closed");
  }
}

run();

In this example, we first create a Binary object from binary data read from a file using fs.readFileSync(). We then use the Binary object to insert the binary data into a MongoDB collection using collection.insertOne().