0

this works

Template.kanjifinder.kanjicharacters = function () {
    console.log("kanjlist");
    return kanji.find({cardid: {$in:[1,2]}}, {sort: {cardid: 1}});  
};

but is there a way to replace this hardcoded array [1,2] with a javascript array?

var str = "123456"
var cardids = str.split("");

if (Meteor.isClient) {
    Template.kanjifinder.kanjicharacters = function () {
        console.log("kanjlist");
        return kanji.find({cardid: {$in: **cardids** }}, {sort: {cardid: 1}});  
    } 
};
2
  • What does the document in your kanji collection look like? Commented Mar 12, 2013 at 17:00
  • well i'm just using cardid here as a test. Ultimately I'll be trying to find Japanese kanji characters instead of the cardid numbers. But I don't want to get bogged down in utf-8 issues for the moment. Basically : cardid: 9 kanji: "中" volume: "Series 2 Volume 1" Commented Mar 12, 2013 at 17:03

1 Answer 1

3

You just need to use a separator in your string, you could do it that way in your code too, but the problem is 2 digit numbers:

var str = "1,2,3,4,5,6" //<<--- use a seperator here
var cardids = str.split(","); //<<--

//remove this line if you intend to use strings in your db instead of integers
cardids = _.map(function(card) { return parseInt(card);});

if (Meteor.isClient) {
   Template.kanjifinder.kanjicharacters = function () {
     console.log("kanjlist");
     return kanji.find({cardid: {$in:cardids}}, {sort: {cardid: 1}});  
   };
}

I'm using underscore to convert the strings to integers, if you don't already have it add it to your meteor package with meteor add underscore

Sign up to request clarification or add additional context in comments.

6 Comments

without the separator you just get a character array (which is what I want). as I wrote it console.log(cardids) = ["1", "2", "3", "4", "5", "6"] . But when I put cardid: {$in:cardids}} i get no results.
I see, the above are strings and mongodb has integers, do you have underscore in your package?
is it a type issue? i.e. $in doesn't like looking for a number in a string?
Ok I'll just try it with the kanji instead of numbers. strings-to-string is my ultimate goal here anyway. just a moment.
Ive edited it to use integers too, that map function will convert the strings to integers
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.