jsonOdm icon indicating copy to clipboard operation
jsonOdm copied to clipboard

$in is not working for array fields

Open dmaziarz opened this issue 3 years ago • 1 comments

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.

dmaziarz avatar Apr 20 '22 09:04 dmaziarz

@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.

konsultaner avatar Apr 21 '22 06:04 konsultaner