daniel ryan
Katalon Apprentice
04/16/2018

how find documents by UUID in a mongo database

here are a few helpers for finding a document by UUID in mongo a database

this first keyword converts a UUID a Binary with a subtype 0x04(required for java/groovy).


def createReadableUUID(String strUUID){
UUID strConUUID = UUID.fromString(strUUID)
long msb = strConUUID.getMostSignificantBits()
long lsb = strConUUID.getLeastSignificantBits()
byte[] uuidBytes = new byte[16];
for (int i = 15; i >= 8; i--) {
uuidBytes[i] = (byte) (lsb & 0xFFL)
lsb >>= 8;
}
for (int i = 7; i >= 0; i--) {
uuidBytes[i] = (byte) (msb & 0xFFL)
msb >>= 8;
}
    return new Binary((byte) 0x04, uuidBytes)
}

this second keyword finds the document based on the above returned UUID

def findMongoDocumentUsingUUID(String strCollection,String strLabel, Binary binUUID)
{
MongoCollection<Document> collection= db.getCollection(strCollection)
BasicDBObject dbObj = new BasicDBObject(strLabel,binUUID)
FindIterable<Document> doc =collection.find(dbObj)
println(doc)
}

To find a document from a mongo collection called 'testcollection', with an '_id' of UUID("a6359605-000a-000a-000a-66f8b86b96af"), invoke the the above keywords like this,

CustomKeywords.'gloMethods.dataBaseMethods.findMongoDocumentUsingUUID'("testcollection", "_id",CustomKeywords.'gloMethods.dataBaseMethods.createReadableUUID'("a6359605-000a-000a-000a-66f8b86b96af") )

hope this helps...this was a bit of a head scratcher for me!!!


Upvote
Quote
Sign In or Register to comment.