Skip to content

Instantly share code, notes, and snippets.

@rizkyabdilah
Created February 16, 2012 11:33
Show Gist options
  • Save rizkyabdilah/1844239 to your computer and use it in GitHub Desktop.
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
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