Monday, 12 December 2011

Testing your document structure for inconsistencies within MongoDB - Part II

In a previous post we looked at how to validate your collection for structural consistency. We did this by creating a DBObject with the 'template' document structure and comparing this against the relevant collection. Testing your document structure for inconsistencies within MongoDB
Now, it's not exactly a leap of faith to extend that and rather than code the template we can define this in a json document. Now all we need to do it to pass the json document and the collection name as parameters saving a recompile for each and every test. Happy days.

{
"_id" : "someGeneratedValue" ,
"nameOne" : "valueOne" ,
"nameTwo" : "valueTwo" ,
"sub" : {
"subNameOne" : "subValueOne" ,
"subNameTwo" : "subValueTwo"} ,
"array" : [
{ "arraySubNameOne" : "arraySubValueOne"} ,
{ "arraySubNameTwo" : "arraySubValueTwo"}
]
}
view raw test.json hosted with ❤ by GitHub


StringBuffer temp = new StringBuffer();
Scanner scanner = new Scanner(new FileReader("pathToFile.json"));
while (scanner.hasNextLine()) {
temp.append(scanner.nextLine());
}
DBObject test = (DBObject)JSON.parse(temp.toString());
view raw conjson.java hosted with ❤ by GitHub

No comments:

Post a Comment