Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noty support encode/decode for MongoDB ObjectId. #54

Open
charleslxh opened this issue Dec 13, 2016 · 2 comments
Open

Noty support encode/decode for MongoDB ObjectId. #54

charleslxh opened this issue Dec 13, 2016 · 2 comments

Comments

@charleslxh
Copy link

// This is a mongodb document.
var doc = {
     _id: ObjectId,
    ....
}
var encodeObjectId = msgpack.encode(doc._id)
var decodeObjectId = msgpack.decode(encodeObjectId)

decodeObjectId is { _bsontype: 'ObjectID', id: 'XOú�H§%¤�ÿ\u0016\u001a' }

@men232
Copy link

men232 commented Feb 19, 2017

const msgpack = require('msgpack-lite');
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

const codec = msgpack.createCodec({});

// Encode ObjectId
codec.addExtPacker(0x3F, ObjectId, (value) => {
	return msgpack.encode(value.toString());
});

// Decode ObjectId
codec.addExtUnpacker(0x3F, (value) => {
	let id = msgpack.decode(value);
	return new ObjectId(id);
});

// After use that
msgpack.encode(data, {codec: codec});
msgpack.decode(data, {codec: codec});

@mykiimike
Copy link

Convert ObjectID to String

var encodeObjectId = msgpack.encode(doc._id.toString())

Or .lean() your query before .exec()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants