Instantly share code, notes, and snippets.

What would you like to do?
Buffer Schema Type in Mongoose: How to specify subtype
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ObjectId = Schema.Types.ObjectId;
const MongooseBuffer = mongoose.Types.Buffer;
const uuid = require('node-uuid');
const db = require('../lib/database');
const BinTestSchema = new Schema({
_id: {
type: Schema.Types.Buffer,
default : () => {
const buffer = uuid.v4(null, new Buffer(16));
return new MongooseBuffer(buffer).toObject(0x03);
get: (buffer) => uuid.unparse(buffer),
set: (string) => {
const buffer = uuid.parse(string);
return new MongooseBuffer(buffer).toObject(0x03);
} // results in: BinData(3,"2S2M2YPQRFGF6S5YTMDoOA==")
module.exports = db.model('BinTest', BinTestSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment