i using auto schema define array field. need find documents multiple specific values contained in array. know can use $in: operator while $in: can match either 1 of value in first array against second array while need match record have value in first array. how can achieve this?
schema definition
demands = new mongo.collection("demands"); var demandschema = new simpleschema({ ability: {type:array}, language: {type: array}}); demands.attachschema(demandschema); contents definition
demandsset=[ {ability: ["laser eye", "rocky skin", "fly"], language: ["english", "latin", "hindu"]}, {ability: ["sky-high jump", "rocky skin", "fly"], language: ["english", "latin", "japanese"]}, {ability: ["rocky skin", "sky-high jump"], language: ["english", "latin", "russian"]} ]; target set
var targetability = ["rocky skin", "fly"]; var targetlanguage = ["english", "hindu"]; when $in operation
demands.find({ $and: [ { ability: { $in: targetability }}, { language: { $in: targetlanguage }} ]}).fetch(); i return me records, while not correct, how can perform such find operation?
$in: not going work because looks any match when comparing 2 arrays, not all elements of 1 array must present in other.
you can write complete javascript functions execute required comparisons inside mongodb query. see $where:
for example:
demands.find({$where: "this.ability.indexof(targetability[0]) > -1 && this.ability.indexof(targetability[1]) > -1 && this.language.indexof(targetlanguage[0]) > -1 && this.language.indexof(targetlanguage[1]) > -1" }); if candidates have other 2 entries each can write more general form of of course.
note meteor apparently does not support function() form of $where: restriction may dated.
also note $where: cannot take advantage of indexes performance may not suitable large collections.
Comments
Post a Comment