mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function (db, module) {
|
|
var helpers = require('../helpers');
|
|
|
|
module.sortedSetRemove = async function (key, value) {
|
|
if (!key) {
|
|
return;
|
|
}
|
|
const isValueArray = Array.isArray(value);
|
|
if (!value || (isValueArray && !value.length)) {
|
|
return;
|
|
}
|
|
|
|
if (isValueArray) {
|
|
value = value.map(helpers.valueToString);
|
|
} else {
|
|
value = helpers.valueToString(value);
|
|
}
|
|
|
|
await db.collection('objects').deleteMany({
|
|
_key: Array.isArray(key) ? { $in: key } : key,
|
|
value: isValueArray ? { $in: value } : value,
|
|
});
|
|
};
|
|
|
|
module.sortedSetsRemove = async function (keys, value) {
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
return;
|
|
}
|
|
value = helpers.valueToString(value);
|
|
|
|
await db.collection('objects').deleteMany({ _key: { $in: keys }, value: value });
|
|
};
|
|
|
|
module.sortedSetsRemoveRangeByScore = async function (keys, min, max) {
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
return;
|
|
}
|
|
var query = { _key: { $in: keys } };
|
|
if (keys.length === 1) {
|
|
query._key = keys[0];
|
|
}
|
|
if (min !== '-inf') {
|
|
query.score = { $gte: parseFloat(min) };
|
|
}
|
|
if (max !== '+inf') {
|
|
query.score = query.score || {};
|
|
query.score.$lte = parseFloat(max);
|
|
}
|
|
|
|
await db.collection('objects').deleteMany(query);
|
|
};
|
|
|
|
module.sortedSetRemoveBulk = async function (data) {
|
|
if (!Array.isArray(data) || !data.length) {
|
|
return;
|
|
}
|
|
var bulk = db.collection('objects').initializeUnorderedBulkOp();
|
|
data.forEach(item => bulk.find({ _key: item[0], value: String(item[1]) }).remove());
|
|
await bulk.execute();
|
|
};
|
|
};
|