Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$in is not working for array fields #2

Open
dmaziarz opened this issue Apr 20, 2022 · 1 comment
Open

$in is not working for array fields #2

dmaziarz opened this issue Apr 20, 2022 · 1 comment

Comments

@dmaziarz
Copy link

dmaziarz commented Apr 20, 2022

Hello,
I'm trying to get all Person with specific hobbyIds, but it seems like $in is not working for array fields. Is there any other option that I can use to query for people with specific hobbyIds?

var odm = jsonOdm;
odm.addSource("people",{
   "Person" : [
       {"id":1,"name":"Richi",jobId:8,hobbyIds:[1]},
       {"id":2,"name":"Dave",jobId:2,hobbyIds:[2,4]},
       {"id":3,"name":"Tom",jobId:3,hobbyIds:[3,5]},
       {"id":4,"name":"Lisa",jobId:4,hobbyIds:[1,2,3]},
       {"id":5,"name":"Hanni",jobId:3,hobbyIds:[1,5]},
       {"id":6,"name":"Selma",jobId:8,hobbyIds:[1,4]},
       {"id":7,"name":"Ralf",jobId:1,hobbyIds:[4,3]}
   ],
   "Jobs" : [
       {"id":8,"name":"plumber"},
       {"id":2,"name":"programmer"},
       {"id":3,"name":"chef"},
       {"id":4,"name":"hairdresser"}
   ],
   "Hobbies" : [
       {"id":1,"name":"swimming"},
       {"id":2,"name":"cycling"},
       {"id":3,"name":"fishing"},
       {"id":4,"name":"coding"},
       {"id":5,"name":"dancing"}
   ]
},true);
// instantiate a collection object
var people = new odm.Collection('Person');

people.$hasOne("jobId","id","Jobs","job");
people.$hasMany("hobbyIds","id","Hobbies","hobbies");

var hairdresser = people.$query()
   .$branch("hobbyIds").$in([1])
   .$all();
console.log(hairdresser);

The code returns an empty array.

@konsultaner
Copy link
Owner

@dmaziarz It doesn't work because $hasMany creates an array of arrays. $hasOne just creates an array. So $in compares

1 === [1]

which failes. The last time I took a deeper look into the code was some years ago. It might take some time to check wether this is a bug or if this is correct behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants