Use MongoDB with custom commands bot

Md Shahriyar Alam

2 months ago

We can use MongoDB database with Commands Commands bot to store, find, update and delete data into a database.

Code

py# initialization of database
db = SlashMongo("database_name")

# insert data
await db.insert_one(
    { "name": "Anything", "age": 21 } # you can insert any data
)

# insert multiple
await db.insert_many([
    { "name": "Anything 1", "age": 21 },
    { "name": "Anything 2", "age": 22 }.
    { "name": "Anything 3", "age": 23 },
])

# find data
data = await db.find({"age": 21}) # it returns a list of data
# [ { "name": "Anything 1", "age": 21 } ]
data = await db.find_one({"age": 21}) # single data in form of an object
# { "name": "Anything 1", "age": 21 }

# delete data
await db.delete_one({"age": 21}) # deletes one row
await db.delete_many({"age": 21}) # deletes multiple row 

# update data
await db.update_one(
    {"age": 21}, 
    { 
        "$set": { "age": 99 } # remember to use $set
    }
)

await db.update_many(
    {"age": 21}, 
    { 
        "$set": { "age": 99 } # remember to use $set
    }
)

Cloud Mongodb

if you have your own mongodb server or you want to use an external mongodb databse you can do so by using the MongoDB function. Then you can use the following code

pydb = MongoDB(
    "connection_url",
    "database_name",
    "collection_name"
)

Rest of the operations are same as SlashMongo

Need help?

If you still need any kind of help, have a suggestion for us or having a question in mind? Feel free to reach us out.

DiscordJoin support server
logoCustom Commands

#1 Custom commands discord bot available on the internet

stripe

Contact

20-22 Wenlock RoadEnglandN1 7GU

© WEiRDSOFT LTD. All rights reserved.