0

Currently, I used MongoVUE to import from current SQL Server database but all PK with uniqueidentifier were converted to something like "Binary - 3:UuidLegacy

enter image description here

My question is how do is create schema for this structure on Mongoose? I can't see Guid/UUID datatype on Mongoose docs http://mongoosejs.com/docs/api.html#schema_Schema.Types

And for more, I get issue when query with ValidationID something like

db.Validations.find({ValidationID: '1389AB5E-56BD-46FD-9A8A-258C7BDE4251'});

It returns nothing although this Guid is exactly same with SQL Server record.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Quoc Nguyen
  • 348
  • 6
  • 25

2 Answers2

0

MongoVUE is obscuring things a bit here, but in a nice way that makes it easier to read. Here's what your example ValidationID of '1389AB5E-56BD-46FD-9A8A-258C7BDE4251' actually looks like - it's type 3 BinData:

{"ValidationID" : BinData(3,"E4mrXla9Rv2aiiWMe95CUQ==")}

The viewer is converting that to a more readable format for you. It's doing that by converting to hex and adding dashes. For proof:

> var bar = BinData(3,"E4mrXla9Rv2aiiWMe95CUQ==")
> bar.hex()
1389ab5e56bd46fd9a8a258c7bde4251

If you want to find that ID, then strip the dashes and pass that into the find as follows (I inserted a sample doc):

> db.foo.find({ValidationID: UUID('1389AB5E56BD46FD9A8A258C7BDE4251')})
{ "_id" : ObjectId("544fd7ddbb4f50c77c61f367"), "ValidationID" : BinData(3,"E4mrXla9Rv2aiiWMe95CUQ==") }

I don't have mongoose set up to test, but have done the leg work in another answer similar to this in terms of converting in javascript.

Community
  • 1
  • 1
Adam Comerford
  • 21,336
  • 4
  • 65
  • 85
0

This drove me crazy for several hours, as a solution I ended up having to install

npm install mongodb --save
npm install slugid --save

and code it as follows

var mongo = require('mongodb');
var slugid = require('slugid');

...

    var guidb64 = slugid.encode(guid);  // guid is something like '8440d561-1127-4fd8-aca9-54de19465d0b'

    guidb64 = guidb64.replace(/_/g, '/'); // for whatever reason slug uses '_' instead of '/' I have in db
    guidb64 += '=='; // adding missing trailing '==' I have in db
    
    var GUID = new mongo.Binary(new Buffer(guidb64, 'base64'), 3);    
    
    var query = MySchemaType.findOne({ Guid: GUID });
    
    query.exec(function(err, entity) {
      // process
    })
Igor Vaschuk
  • 2,794
  • 1
  • 19
  • 11