i have mongodb collection documents in following format:
{ "_id" : objectid("4e8ae86d08101908e1000001"), "name" : ["name"], "zipcode" : ["2223"] } { "_id" : objectid("4e8ae86d08101908e1000002"), "name" : ["another ", "name"], "zipcode" : ["2224"] } i can documents match specific array size:
db.accommodations.find({ name : { $size : 2 }}) this correctly returns documents 2 elements in name array. however, can't $gt command return documents name field has array size of greater 2:
db.accommodations.find({ name : { $size: { $gt : 1 } }}) how can select documents name array of size greater 1 (preferably without having modify current data structure)?
update:
for mongodb versions 2.2+ more efficient way described @johnnyhk in answer.
1.using $where
db.accommodations.find( { $where: "this.name.length > 1" } ); but...
javascript executes more native operators listed on page, flexible. see server-side processing page more information.
2.create extra field namesarraylength, update names array length , use in queries:
db.accommodations.find({"namesarraylength": {$gt: 1} }); it better solution, , work faster (you can create index on it).
Comments
Post a Comment