'How to search the database so that it does not show certain things (I do not know how to write it)

Discord.js 13.6.0 | Node.js 16.14.2 | Mongoose 6.2.3

I want to do so that my bot searches the database to see if the guild should be removed or added

Example

I have 524 documents and 523 guilds

I want to check which document should be deleted

 const guilds = await this.client.guildsData.find() // This is my database
 const guilds2 = await this.client.guilds.cache.map(guild => guild.id); (This is all my bot guilds)

 const lb = await guilds
    .filter((r) => r.id !== `${guilds2}`)
    .sort((a, b) => b.id - a.id).map((r) => r)
    .map((r, i) => `${r.id}`)

 const embed = new Discord.MessageEmbed()
    .setDescription(`${lb.join(", ")}`)

 message.reply({ embeds: [embed] })

Anyone can help with it?

Btw this work

 const guilds = await this.client.guildsData.find() // This is my database

 const lb = await guilds
    .filter((r) => r.id !== "784242201344737292" && r.id !== "743513755060797501" && r.id !== "another ID")
    .sort((a, b) => b.id - a.id).map((r) => r)
    .map((r, i) => `${r.id}`)

 const embed = new Discord.MessageEmbed()
    .setDescription(`${lb.join(", ")}`)

 message.reply({ embeds: [embed] })

But I don't want to add or remove ID's all the time + I have a lot of servers and it would take a while before I add all of them

Sorry for my eng and explanation



Solution 1:[1]

You should be able to use .find() with an object to filter the results for you.

See https://mongoosejs.com/docs/api.html#model_Model.find

If you're just looking for a guild with one specific ID, then try .findOne({id: guild_id_youre_looking_for})

To go even further, if your sole goal is just to delete the document, then just use .deleteOne({id: guid_id_here})

Hope this helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wubzy