Last active
August 29, 2015 14:20
-
-
Save jwerre/1f7138fb98b449e43e6d to your computer and use it in GitHub Desktop.
MongoDb negative look-ahead regex search test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var coll = [ | |
{ | |
"str": "batman", | |
"nested": [ | |
{ | |
"value": "batman" | |
}, | |
{ | |
"value": "spiderman" | |
}, | |
{ | |
"value": "superman" | |
}, | |
{ | |
"value": "green lantern" | |
} | |
], | |
"arr": ["batman","spiderman","superman","green lantern"] | |
}, | |
{ | |
"str": "sd", | |
"nested": [ | |
{ | |
"value": "sd" | |
}, | |
{ | |
"value": "sd" | |
}, | |
{ | |
"value": "sdsd" | |
}, | |
{ | |
"value": "sdsdsd" | |
} | |
], | |
"arr": ["sd","sd","sdsd","sdsdsd"] | |
}, | |
{ | |
"str": "apple", | |
"nested": [ | |
{ | |
"value": "apple" | |
}, | |
{ | |
"value": "peach" | |
}, | |
{ | |
"value": "banana" | |
}, | |
{ | |
"value": "pear" | |
} | |
], | |
"arr": ["apple","peach","banana","pear"] | |
}, | |
{ | |
"str": "dsf", | |
"nested": [ | |
{ | |
"value": "dsf" | |
}, | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
} | |
], | |
"arr": ["dsf","sdf","sdf","sdf"] | |
}, | |
{ | |
"str": "sdf", | |
"nested": [ | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdfsd" | |
} | |
], | |
"arr": ["sdf","sdf","sdf","sdfsd"] | |
}, | |
{ | |
"str": "Hello", | |
"nested": [ | |
{ | |
"value": "Hello" | |
}, | |
{ | |
"value": "Hello" | |
}, | |
{ | |
"value": "Hhh" | |
}, | |
{ | |
"value": "Hhjj" | |
} | |
], | |
"arr": ["Hello","Hello","Hhh","Hhjj"], | |
}, | |
{ | |
"str": "apple", | |
"nested": [ | |
{ | |
"value": "apple" | |
}, | |
{ | |
"value": "peach" | |
}, | |
{ | |
"value": "banana" | |
}, | |
{ | |
"value": "orange" | |
} | |
], | |
"arr": ["apple","peach","banana","orange"] | |
}, | |
{ | |
"str": "", | |
"nested": [ | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "d" | |
} | |
], | |
"arr": ["","", "","d"] | |
}, | |
{ | |
"str": "apple", | |
"nested": [ | |
{ | |
"value": "apple" | |
}, | |
{ | |
"value": "peach" | |
}, | |
{ | |
"value": "banana" | |
}, | |
{ | |
"value": "orange" | |
} | |
], | |
"arr": ["apple","peach","banana","orange"] | |
}, | |
{ | |
"str": "apple", | |
"nested": [ | |
{ | |
"value": "apple" | |
}, | |
{ | |
"value": "peach" | |
}, | |
{ | |
"value": "banana" | |
}, | |
{ | |
"value": "orange" | |
} | |
], | |
"arr": ["apple","peach","banana","orange"] | |
}, | |
{ | |
"str": "BBBBBBBBBBBBBBBBBBBBBBBBBB", | |
"nested": [ | |
{ | |
"value": "BBBBBBBBBBBBBBBBBBBBBBBBBB" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
} | |
], | |
"arr": ["BBBBBBBBBBBBBBBBBBBBBBBBBB","", "",""] | |
}, | |
{ | |
"str": "jiojo", | |
"nested": [ | |
{ | |
"value": "jiojo" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
} | |
], | |
"arr": ["jiojo","", "",""] | |
}, | |
{ | |
"str": "", | |
"nested": [ | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "fyf" | |
}, | |
{ | |
"value": "" | |
}, | |
{ | |
"value": "" | |
} | |
], | |
"arr": ["","fyf", "",""] | |
}, | |
{ | |
"str": "sdf", | |
"nested": [ | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
}, | |
{ | |
"value": "sdf" | |
} | |
], | |
"arr": ["sdf","sdf","sdf","sdf"] | |
}, | |
{ | |
"str": "dfg", | |
"nested": [ | |
{ | |
"value": "dfg" | |
}, | |
{ | |
"value": "dfg" | |
}, | |
{ | |
"value": "dfg" | |
}, | |
{ | |
"value": "dfg" | |
} | |
], | |
"arr": ["dfg","dfg","dfg","dfg"] | |
}, | |
{ | |
"str": "as", | |
"nested": [ | |
{ | |
"value": "as" | |
}, | |
{ | |
"value": "sdd" | |
}, | |
{ | |
"value": "sda" | |
}, | |
{ | |
"value": "ssd" | |
} | |
], | |
"arr": ["as","sdd","sda","ssd"] | |
}, | |
{ | |
"str": "me", | |
"nested": [ | |
{ | |
"value": "me" | |
}, | |
{ | |
"value": "you" | |
}, | |
{ | |
"value": "him" | |
}, | |
{ | |
"value": "her" | |
} | |
], | |
"arr": ["me","you","him","her"] | |
}, | |
{ | |
"str": "apple", | |
"nested": [ | |
{ | |
"value": "apple" | |
}, | |
{ | |
"value": "peach" | |
}, | |
{ | |
"value": "banana" | |
}, | |
{ | |
"value": "orange" | |
} | |
], | |
"arr": ["apple","peach","banana","orange"] | |
}, | |
{ | |
"str": " apple", | |
"nested": [ | |
{ | |
"value": " apple" | |
}, | |
{ | |
"value": "peach" | |
}, | |
{ | |
"value": "banana" | |
}, | |
{ | |
"value": "orange" | |
} | |
], | |
"arr": [" apple","peach","banana","orange"] | |
}, | |
{ | |
"str": "orange", | |
"nested": [ | |
{ | |
"value": "orange" | |
}, | |
{ | |
"value": "banaan" | |
}, | |
{ | |
"value": "peaches" | |
}, | |
{ | |
"value": "soemthing else" | |
} | |
], | |
"arr": ["orange","banaan","peaches","soemthing else"] | |
} | |
] | |
db.testCollection.drop(); | |
db.testCollection.insert(coll); | |
print("\n\n\nMongoDB negative look-ahead $regex search test") | |
var total = db.testCollection.count(); | |
print("\nTotal Documents: "+total); | |
var testQuery = {$not:{$eq:'apple'}} | |
var lookAheadRegEx = /^(?!apple).*/i; | |
print("\nRegex test 1 passed: ", !lookAheadRegEx.test('apple') ); | |
print("\nRegex test 2 passed: ", lookAheadRegEx.test('peach') ); | |
var expectedCount = db.testCollection.count({ 'str': testQuery }); | |
var regExCount = db.testCollection.count({'str':lookAheadRegEx}); | |
var passed = expectedCount === regExCount | |
print("\nString Test Passed:", passed) | |
if(!passed){ | |
print("\tThe total docuements for negative look-ahead regex search on a string should be "+expectedCount+ " and got "+regExCount); | |
} | |
var expectedCount = db.testCollection.count({ 'arr': testQuery }); | |
var regExCount = db.testCollection.count({'arr':lookAheadRegEx}); | |
var passed = expectedCount === regExCount | |
print("\nArray Test Passed:", passed) | |
if(!passed){ | |
print("\tThe total docuements for negative look-ahead regex search on an array should be "+expectedCount+ " and got "+regExCount); | |
} | |
var expectedCount = db.testCollection.count({ 'nested.value': testQuery }); | |
var regExCount = db.testCollection.count({'nested.value':lookAheadRegEx}); | |
var passed = expectedCount === regExCount | |
print("\nNested Collection Test Passed:", passed) | |
if(!passed){ | |
print("\tThe total docuements for negative look-ahead regex search on a nested collection should be "+expectedCount+ " and got "+regExCount); | |
} | |
print("\n\n\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment