mongodb - Check if value exist into array field mongoid rails -
i have groups field in mongodb database
"groups" : "[\"5514efcc6970640f40150100\", \"5514efcc6970640f40160100\", \"5514efcc6970640f40170100\", \"5514efcc6970640f40180100\"] please suggest query check if field exist above database array.
like modelname.find(:groups=> 5514efcc6970640f40150100)
looks have string in groups, not array. string looks called inspect on array of bson objectids in ruby , stored stringified array in mongodb.
the leading double quote here:
⬇ "groups" : "[\"5514efcc6970640f40150100\", ... ⬆ tells you have string. inside string have looks array of strings still string.
if that's have think you're stuck regex searches (which slow), like:
model.where(:groups => /"5514efcc6970640f40150100"/) that assumes groups strings consistently formed , stringified arrays of hexadecimal strings.
if case i'd suggest fix database , code use real arrays in groups.
if groups array of hex strings like:
"groups" : ["5514efcc6970640f40150100", ... then can query scalar field , let mongodb unwrap array you:
model.where(:groups => '5514efcc6970640f40150100')
Comments
Post a Comment