I am creating a Discord Bot, this is a problem I faced when I try to create an Array of data for MongoDB.
I don’t have a bigger knowledge of MongoDB. So actually I don’t know how to work with MongoDB arrays.
CountersSchema.js
const {Schema, model} = require('mongoose');
const countersSchema = new Schema({
Guild: { type: String, required: true },
Counters: [
{
Names: { type: String, required: true },
Types: { type: String, required: true },
Channels: { type: String, required: true }
}
]
});
module.exports = model(`counters`, countersSchema);
I tried to create an array in here. I’m not sure whether this is correct or wrong.
The thing I want to do is, let users create counters for their Discord servers. And I want them to only allow max 5 counters per server. So I created this schema to store that data for each counter they create.
I want this Guild
value to be unique. which is the id of the server. And I want Names
, Types
& Channels
to be an arrays of up to 5 values and falls under the Guild
value.
Counters.js
const counterChannels = require('../../Schemas/CountersSchema');
const data = async (interaction) => {
try {
return await counterChannels.findOne({ Guild: interaction.guild.id });
} catch (error) {
console.error('Error while fetching counters:', error);
return null;
}
};
const existingCounter = await data(interaction);
if (existingCounter) {
// Check if the maximum number of counters is reached
if (existingCounter.Counters.length >= 5) {
return;
};
await counterChannels.updateOne(
{ Guild: guild.id },
{
$push: {
Counters: {
Names: givenName, // givenName Declaration not included.
Types: counterType, // counterType Declaration not included.
Channels: createdCounter.id // createdCounter Declaration not included.
}
}
}
);
} else {
await counterChannels.create({
Guild: guild.id,
Counters: [{
Names: givenName,
Types: counterType,
Channels: createdCounter.id
}]
});
}
This is only a part of the Counters.js file. This is how I tried to find if there are any previous data is available in the db.
And if no data available, I want to create a new entry under the guild id and assign these 3 values to that array.
If previous data available, I only want to assign new values to the array and keep the Guild
as it is.
So, I want your help in here to correctly do this. I don’t think I have done this correctly.
There is one more thing. That is how to call the data using the array.
ServerCounters.js
const serverCounters = require('../Schemas/CountersSchema');
async execute (member) {
const data = await serverCounters.find({Guild: member.guild.id});
if(!data.length) return;
const counterType = data.map(doc => doc.Types);
const counterChannel = data.map(doc => doc.Channels);
const counterName = data.map(doc => doc.Names);
}
in this code, I want to check is there counter data available for the server. just like the previous Counters.js file. After that, I want to devide the data according to the values of Types
. So let’s say if one of the values in Types
array is 'Total Members'
, then I want to get the values of the other two arrays that are included along with the 'Total Members'
for Types
.
So, this is what I’m looking for the help with.
- Create 3 arrays with up to 5 different values. Named
Types
,Names
, andChannels
under theGuild
. - Check if there are already 5 values for that 3 arrays under the interacting guild’s id. (in Counters.js)
- If no data available, create new entry for that guild.
- If less than 5 values available, insert current data to that 3 arrays also.
- Find corresponding values of the
Names
andChannels
arrays using values ofTypes
array.