Skip to content

Commit b88b0c5

Browse files
JeremyPleaseflovilmart
authored andcommitted
Fix multiple use of notEqualTo (#2882)
* Add failing test for multiple .notEqualTo on relation with same class * Fix multiple .notEqualTo on relations with the same class Multiple should use the union of all objectIds not the intersect Fixes #1596
1 parent d8ba9e8 commit b88b0c5

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

spec/ParseQuery.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ describe('Parse.Query testing', () => {
127127
return query.find().then(function(results){
128128
equal(results.length, 0);
129129
});
130+
}).then(function(){
131+
var query = new Parse.Query(Cake);
132+
query.notEqualTo("hater", user2);
133+
query.notEqualTo("liker", user2);
134+
// user2 doesn't like any cake so this should be 0
135+
return query.find().then(function(results){
136+
equal(results.length, 0);
137+
});
138+
}).then(function(){
139+
var query = new Parse.Query(Cake);
140+
query.equalTo("hater", user);
141+
query.equalTo("liker", user);
142+
// user doesn't hate any cake so this should be 0
143+
return query.find().then(function(results){
144+
equal(results.length, 0);
145+
});
130146
}).then(function(){
131147
done();
132148
}).catch((err) => {

src/Controllers/DatabaseController.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -676,17 +676,12 @@ DatabaseController.prototype.addInObjectIdsIds = function(ids = null, query) {
676676
return query;
677677
}
678678

679-
DatabaseController.prototype.addNotInObjectIdsIds = function(ids = null, query) {
680-
let idsFromNin = query.objectId && query.objectId['$nin'] ? query.objectId['$nin'] : null;
681-
let allIds = [idsFromNin, ids].filter(list => list !== null);
682-
let totalLength = allIds.reduce((memo, list) => memo + list.length, 0);
679+
DatabaseController.prototype.addNotInObjectIdsIds = function(ids = [], query) {
680+
let idsFromNin = query.objectId && query.objectId['$nin'] ? query.objectId['$nin'] : [];
681+
let allIds = [...idsFromNin,...ids].filter(list => list !== null);
683682

684-
let idsIntersection = [];
685-
if (totalLength > 125) {
686-
idsIntersection = intersect.big(allIds);
687-
} else {
688-
idsIntersection = intersect(allIds);
689-
}
683+
// make a set and spread to remove duplicates
684+
allIds = [...new Set(allIds)];
690685

691686
// Need to make sure we don't clobber existing shorthand $eq constraints on objectId.
692687
if (!('objectId' in query)) {
@@ -696,8 +691,8 @@ DatabaseController.prototype.addNotInObjectIdsIds = function(ids = null, query)
696691
$eq: query.objectId
697692
};
698693
}
699-
query.objectId['$nin'] = idsIntersection;
700694

695+
query.objectId['$nin'] = allIds;
701696
return query;
702697
}
703698

0 commit comments

Comments
 (0)