Query all the objects by key.
-
Let's say I have a series of objects stored in my database (mongodb) and they all share the same key. For example:
_key = "pluginname:users:customstats"
How can I get all the objects that share that same key?
I've tried something like this:
const data = await db.getObjects(["pluginname:users:customstats"], ["uid", "score"]); return data;
But the query only gets the very last objecet saved in the database instead of all the ones I've created.
-
They all will have the same key to be fetched. I ended up just creating two new query methods in the nodebb code cause I couldn't find a solution for my problem:
module.mget_type = async function (type) { const data = await module.client.collection('objects').find( { type }, { projection: { _id: 0 } } ).toArray(); return data; }; module.mget_field_value = async function (field, value) { const data = await module.client.collection('objects').find( { [field]: value }, { projection: { _id: 0 } } ).toArray(); return data; };
-
I would advise against using those since they operate on fields that are not indexed. If all you want to store is a uid and score pair you can use sorted sets like below.
//setting values await db.sortedSetAdd('pluginname:users:customstats', [score1, score2], [uid1, uid2]); // getting first 10 values await db.getSortedSetRevRange('pluginname:users:customstats', 0, 9);
Copyright © 2024 NodeBB | Contributors