-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
containedIn on an array type key #4762
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
Comments
This is the expected behavior as containedIn operator in mongodb behaves this way. If you’re looking for and exact match, use the equal operator |
Thanks @flovilmart i'm not looking for exact match, i'm looking for |
i believe contains all should do the trick, can you provide the logs when running with VERBOSE=1? |
I tried with here is the verbose
|
You’re matching the zip code or the offers? |
offers is my array of |
I tried
but none are matching all my |
I added a test that should reproduce your issue but it doesn't seem to fail: Can you have a look and check whether this is what you were looking for? |
thanks for the test @flovilmart to recreate the case you should request an All with more objects. so that would be
with query
and the output should be object 1 : |
I added an additional test, and both matches as you would expect. Closing now as I can't reproduce the issue with the provided information. Feel free to open a PR with the failing tests. |
Thanks @flovilmart here is the case to reproduce const objects = [1,2,3,4,5,6,7,8].map((idx) => {
const obj = new Parse.Object('Object');
obj.set('key', idx);
return obj;
});
let parent;
let parent3;
Parse.Object.saveAll(objects).then(() => {
parent = new Parse.Object('Parent');
parent.set('objects', objects.slice(0, 3));
const parent2 = new Parse.Object('Parent');
parent2.set('objects', [objects[1]]);
parent3 = new Parse.Object('Parent');
parent3.set('objects', objects.slice(1, 4));
return Parse.Object.saveAll([parent, parent2, parent3]);
}).then(() => {
const query = new Parse.Query('Parent');
query.containedIn('objects', objects);
return query.find();
}).then((result) => {
expect(result[0].id).not.toBeUndefined();
expect(result[1].id).not.toBeUndefined();
expect(result[2].id).not.toBeUndefined();
expect(result.length).toBe(3);
}).then(done).catch(done.fail); |
so we want to have all parent where |
@jeremypiednoel did you test your case because it works as expected. |
Indeed it's working ! |
No problem! Good luck! |
Ok i found why the key is to add a value in a parent const objects = [1,2,3,4,5,6,7,8].map((idx) => {
const obj = new Parse.Object('Object');
obj.set('key', idx);
return obj;
});
let parent;
let parent3;
Parse.Object.saveAll(objects).then(() => {
parent = new Parse.Object('Parent');
parent.set('objects', objects.slice(0, 3));
let shift = objects.shift();
// parent2 shouldn't be in the result because shift isn't in the initial array anymore
const parent2 = new Parse.Object('Parent');
parent2.set('objects', [objects[1],shift]);
parent3 = new Parse.Object('Parent');
parent3.set('objects', objects.slice(1, 4));
return Parse.Object.saveAll([parent, parent2, parent3]);
}).then(() => {
const query = new Parse.Query('Parent');
query.containedIn('objects', objects);
return query.find();
}).then((result) => {
expect(result[0].id).not.toBeUndefined();
expect(result[1].id).not.toBeUndefined();
expect(result.length).toBe(2);
}).then(done).catch(done.fail); |
@jeremypiednoel I just tested it again and with Keeping your code as is, Again, Everyting is good on parse-server side, you should have a look at your logic somewhere or write unit tests in your project as it is not impossible to reproduce the issue. |
If you keep the |
it should be because
|
Yes so |
it matches because there's at least one matching, not all matching one in the provided array. If you're using containsAll, that's a different story. |
But when i use containsAll it makes sure that all |
Because if i follow your logic const objects = [1,2,3,4,5,6,7,8].map((idx) => {
const obj = new Parse.Object('Object');
obj.set('key', idx);
return obj;
});
let parent;
let parent3;
Parse.Object.saveAll(objects).then(() => {
parent = new Parse.Object('Parent');
parent.set('objects', objects.slice(0, 3));
let shift = objects.shift();
// parent2 shouldn't be in the result because shift isn't in the initial array anymore
const parent2 = new Parse.Object('Parent');
parent2.set('objects', [objects[1],shift]);
parent3 = new Parse.Object('Parent');
parent3.set('objects', objects.slice(1, 4));
return Parse.Object.saveAll([parent, parent2, parent3]);
}).then(() => {
const query = new Parse.Query('Parent');
query.containsAll('objects', objects);
return query.find();
}).then((result) => {
expect(result[0].id).not.toBeUndefined();
expect(result[1].id).not.toBeUndefined();
expect(result.length).toBe(2);
}).then(done).catch(done.fail); |
No it should not because none of the Parent object contains all passed objects. |
Ok so my question is how can i make sure that i return object where parent.objects are all contains into the initial array. |
That would be the containsAll, as tested in the first test. |
The contains all make sure full |
So there’s no operator to achieve that directly it seems. Besides the ‘equalTo’ operator. |
When i try with the
|
Which makes sense as equals doesn't support array transformations and even less with pointers. In any case, all the examples that were implemented today are working and from what I understand, what you're trying to do is either unimplemented or unsupported / undocumented. If it's an additional feature that you need, feel free to open a PR providing it. If that's an issue as it's documented somewhere in the guides somewhere, feel free to reopen one. |
Thanks i'll make some research in parse source code and mongodb queries to see if i can find or implement it |
Thanks! Good luck if you need any help, feel free to reach out! |
Hey @flovilmart i finally found how to do it using |
That’s interesting! Do you have an idea of how it would be named etc before you get started? Perhaps with a PR on the docs first so we can discuss the API for JS and REST? |
I would say |
It may be confusing with containedIn, isn’t it arrayMatches? |
yes |
I created the PR #4766 to just add the mongo elements |
* Adding elemMatch and nor * lint * adding test * adding edge test * postgres support * clean up * empty test
…parse-community#4766) * Adding elemMatch and nor * lint * adding test * adding edge test * postgres support * clean up * empty test
Hi Guys !
Issue Description
I'm trying to use
containedIn
on an array key :My key arrayKey is an array in the DB : for instance
[1, 3];
My issue is the query will return objects where at least one item from array is in arrayKey and not all of them.
I'm not sure if it's the normal behavior.
If it is do you guys know a workaround ? because
containsAll
won't work either.Thanks for the reading.
Steps to reproduce
Make a
containedIn
query on array key.Environment Setup
2.7.4
macOS 10.12.6 (Sierra)
iMac (Retina 5K, 27-inch, Late 2015)
Localhost
The text was updated successfully, but these errors were encountered: