feat: convert queries so they used indices directly

v1.18.x
Barış Soner Uşaklı 5 years ago
parent a532e2bb83
commit 12c6bc2e07

@ -306,7 +306,11 @@ module.exports = function (module) {
return;
}
value = helpers.valueToString(value);
const result = await module.client.collection('objects').findOne({ _key: key, value: value }, { projection: { _id: 0, _key: 0, score: 0 } });
const result = await module.client.collection('objects').findOne({
_key: key, value: value,
}, {
projection: { _id: 0, value: 1 },
});
return !!result;
};
@ -315,7 +319,11 @@ module.exports = function (module) {
return;
}
values = values.map(helpers.valueToString);
const results = await module.client.collection('objects').find({ _key: key, value: { $in: values } }, { projection: { _id: 0, _key: 0, score: 0 } }).toArray();
const results = await module.client.collection('objects').find({
_key: key, value: { $in: values },
}, {
projection: { _id: 0, value: 1 },
}).toArray();
var isMember = {};
results.forEach(function (item) {
@ -332,7 +340,11 @@ module.exports = function (module) {
return [];
}
value = helpers.valueToString(value);
const results = await module.client.collection('objects').find({ _key: { $in: keys }, value: value }, { projection: { _id: 0, score: 0 } }).toArray();
const results = await module.client.collection('objects').find({
_key: { $in: keys }, value: value,
}, {
projection: { _id: 0, _key: 1, value: 1 },
}).toArray();
var isMember = {};
results.forEach(function (item) {
@ -351,7 +363,7 @@ module.exports = function (module) {
const data = await module.client.collection('objects').find({
_key: keys.length === 1 ? keys[0] : { $in: keys },
}, { projection: { _id: 0, score: 0 } }).sort({ score: 1 }).toArray();
}, { projection: { _id: 0, _key: 1, value: 1 } }).toArray();
var sets = {};
data.forEach(function (set) {

@ -10,7 +10,6 @@ module.exports = function (module) {
{ $match: { _key: { $in: keys } } },
{ $group: { _id: { value: '$value' } } },
{ $group: { _id: null, count: { $sum: 1 } } },
{ $project: { _id: 0, count: '$count' } },
]).toArray();
return Array.isArray(data) && data.length ? data[0].count : 0;
};

@ -992,21 +992,12 @@ describe('Sorted Set methods', function () {
});
});
it('should remove value from multiple keys', function (done) {
db.sortedSetAdd('multiTest3', [1, 2, 3, 4], ['one', 'two', 'three', 'four'], function (err) {
assert.ifError(err);
db.sortedSetAdd('multiTest4', [3, 4, 5, 6], ['three', 'four', 'five', 'six'], function (err) {
assert.ifError(err);
db.sortedSetRemove(['multiTest3', 'multiTest4'], 'three', function (err) {
assert.ifError(err);
db.getSortedSetsMembers(['multiTest3', 'multiTest4'], function (err, members) {
assert.ifError(err);
assert.deepEqual(members, [['one', 'two', 'four'], ['four', 'five', 'six']]);
done();
});
});
});
});
it('should remove value from multiple keys', async function () {
await db.sortedSetAdd('multiTest3', [1, 2, 3, 4], ['one', 'two', 'three', 'four']);
await db.sortedSetAdd('multiTest4', [3, 4, 5, 6], ['three', 'four', 'five', 'six']);
await db.sortedSetRemove(['multiTest3', 'multiTest4'], 'three');
assert.deepStrictEqual(await db.getSortedSetRange('multiTest3', 0, -1), ['one', 'two', 'four']);
assert.deepStrictEqual(await db.getSortedSetRange('multiTest4', 0, -1), ['four', 'five', 'six']);
});
it('should remove multiple values from multiple keys', function (done) {

Loading…
Cancel
Save