Created
February 16, 2012 11:33
-
-
Save rizkyabdilah/1844239 to your computer and use it in GitHub Desktop.
use $where in mongodb to get query length of array, because query $size not support $gt, $lt, $lte and $gte
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
db.test.insert({'name': 'rizky', 'abilities': ['eat', 'sleep', 'code']}); | |
db.test.insert({'name': 'abdilah', 'abilities': ['drink']}); | |
db.test.insert({'name': 'median', 'abilities': null}); | |
condition_have_ability_more_than_2 = function(){ | |
return typeof(this.abilities) == "object" && this.abilities != null && this.abilities.length > 2; | |
} | |
// peoples have abilities more_than_2 | |
db.test.find(condition_have_ability_more_than_2); | |
// update data | |
db.test.find(condition_have_ability_more_than_2).forEach( | |
function(v){ | |
v['smart'] = true; | |
db.test.save(v); | |
} | |
); | |
// check success update or not | |
db.test.findOne({smart: true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment