mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
feat: convert queries so they used indices directly
This commit is contained in:
@@ -306,7 +306,11 @@ module.exports = function (module) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
value = helpers.valueToString(value);
|
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;
|
return !!result;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -315,7 +319,11 @@ module.exports = function (module) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
values = values.map(helpers.valueToString);
|
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 = {};
|
var isMember = {};
|
||||||
results.forEach(function (item) {
|
results.forEach(function (item) {
|
||||||
@@ -332,7 +340,11 @@ module.exports = function (module) {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
value = helpers.valueToString(value);
|
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 = {};
|
var isMember = {};
|
||||||
results.forEach(function (item) {
|
results.forEach(function (item) {
|
||||||
@@ -351,7 +363,7 @@ module.exports = function (module) {
|
|||||||
|
|
||||||
const data = await module.client.collection('objects').find({
|
const data = await module.client.collection('objects').find({
|
||||||
_key: keys.length === 1 ? keys[0] : { $in: keys },
|
_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 = {};
|
var sets = {};
|
||||||
data.forEach(function (set) {
|
data.forEach(function (set) {
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ module.exports = function (module) {
|
|||||||
{ $match: { _key: { $in: keys } } },
|
{ $match: { _key: { $in: keys } } },
|
||||||
{ $group: { _id: { value: '$value' } } },
|
{ $group: { _id: { value: '$value' } } },
|
||||||
{ $group: { _id: null, count: { $sum: 1 } } },
|
{ $group: { _id: null, count: { $sum: 1 } } },
|
||||||
{ $project: { _id: 0, count: '$count' } },
|
|
||||||
]).toArray();
|
]).toArray();
|
||||||
return Array.isArray(data) && data.length ? data[0].count : 0;
|
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) {
|
it('should remove value from multiple keys', async function () {
|
||||||
db.sortedSetAdd('multiTest3', [1, 2, 3, 4], ['one', 'two', 'three', 'four'], function (err) {
|
await db.sortedSetAdd('multiTest3', [1, 2, 3, 4], ['one', 'two', 'three', 'four']);
|
||||||
assert.ifError(err);
|
await db.sortedSetAdd('multiTest4', [3, 4, 5, 6], ['three', 'four', 'five', 'six']);
|
||||||
db.sortedSetAdd('multiTest4', [3, 4, 5, 6], ['three', 'four', 'five', 'six'], function (err) {
|
await db.sortedSetRemove(['multiTest3', 'multiTest4'], 'three');
|
||||||
assert.ifError(err);
|
assert.deepStrictEqual(await db.getSortedSetRange('multiTest3', 0, -1), ['one', 'two', 'four']);
|
||||||
db.sortedSetRemove(['multiTest3', 'multiTest4'], 'three', function (err) {
|
assert.deepStrictEqual(await db.getSortedSetRange('multiTest4', 0, -1), ['four', 'five', 'six']);
|
||||||
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 multiple values from multiple keys', function (done) {
|
it('should remove multiple values from multiple keys', function (done) {
|
||||||
|
|||||||
Reference in New Issue
Block a user