uuid-mongodb
Generates and parses BSON UUIDs for use with MongoDB. BSON UUIDs provide better performance than their string counterparts.
Inspired by @srcagency's mongo-uuid
Install
npm install uuid-mongodb
Usage
const MUUID = require('uuid-mongodb');
# Create a v1 binary UUID
const mUUID1 = MUUID.v1();
# Create a v4 binary UUID
const mUUID4 = MUUID.v4();
# Print a string representation of a binary UUID
mUUID1.toString()
# Create a binary UUID from a valid uuid string
const mUUID2 = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459')
# Create a binary UUID from a MongoDb Binary
# This is useful to get MUUIDs helpful toString() method
const mUUID3 = MUUID.from()
Examples
Query using binary UUIDs
const uuid = MUUID.from('393967e0-8de1-11e8-9eb6-529269fb1459');
return collection.then(c =>
c.findOne({
_id: uuid,
})
);
Work with binary UUIDs returned in query results
return collection
.then(c => c.findOne({ _id: uuid }))
.then(doc => {
const uuid = MUUID.from(doc._id).toString();
});
Notes
Currently supports UUID v1 and v4
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT