mongodb - projection document querying fields containing certain letters -
i trying menuitems.find(..
return documents field "group" has letter contained in "menugroup"
example:
if menugroup "a" return documents group equals a.
if menugroup "ab" return documents group field equals "a" documents group field equals "b".
reading mongo docs, can't figure out. please help? thanks
var items = [ {menuitem: "task1", group: "a"}, {menuitem: "task2", group: "a"}, {menuitem: "task3", group: "b"}, {menuitem: "task4", group: "a"}, {menuitem: "task5", group: "a"}, {menuitem: "task6", group: "a"}, {menuitem: "task7", group: "b"}, {menuitem: "task8", group: "b"}, {menuitem: "task9", group: "b"}, {menuitem: "login", group: "a"}, {menuitem: "logout", group: "a"} ] if (!menuitems.find().count()) { _.each(items, function (doc) { menuitems.insert(doc); }) } meteor.publish('menuitems', function () { var menugroup; if (!this.userid) { //no 1 logged in menugroup = 'a'; } else { menugroup = meteor.users.findone({_id: this.userid}).profile.menugroup; } return menuitems.find({group: menugroup}); //what if menugroup "ab" });
one way solve split menugroup
, use mongo's $in operator this:
return menuitems.find({group: {$in: menugroup.split('')}});
so if menugroup
'ab'
selector end being {group: {$in: ['a', 'b']}}
.
Comments
Post a Comment