#mongodb

18 messages · Page 1 of 1 (latest)

languid bane
#

check if there is already exist document with that user id in the database

#

try to find document where userID is the id which was mentioned and if there is such data just don't add it again

grizzled tusk
#

yeah but I cannot always check the database

#

I want it to reply with a message eg. this user already exists in db

languid bane
#

to quickly check without doing query search

languid bane
dusky umbra
#

<schema>.findOne({userID: user?.id}) will return null if no document

#

Or you can always use findOneAndUpdate

grizzled tusk
#

I made sth like this

#
    let user1 = await UserData.findOne({ userID: user});
      if (user1) {
        message.channel.send("User Already Exists")
      }
#

thx @dusky umbra

languid bane
#
let userId = message.mentions.members.first().id ?? message.guild.members.cache.get(args[0]);
let userData = await UserData.findOne({ userID: userId });

if (userData) {
  message.channel.send("User Already Exists")
}

else {
  ... create the data
}

grizzled tusk
#

yeah I had the other one ready

languid bane
#

👍

grizzled tusk
#

I just did not search so much about mongo