Tutorial by: Adam


► In this tutorial I demonstrate how to connect to a MongoDB database, add a document, update a document and retrieve a document.

on script load:
    set {mongoCluster} to mongo server with connection string "mongodb+srv://admin:admin@cluster0.xc5kh.mongodb.net/database?retryWrites=true&w=majority"
    set {mongoDatabase} to mongo database "database" of {mongoCluster}
    set {mongoCollection} to mongo collection "collection" of {mongoDatabase}

on join:
    set {_doc} to a new mongo document
    set mongo value "uuid" of {_doc} to player's uuid
    set mongo value "messages" of {_doc} to 0
    insert mongo document {_doc} into collection {mongoCollection}

on chat:
    add 1 to {%player's uuid%::messages}

command update:
    trigger:
        set {_doc} to first mongo document with mongosk filter where field "uuid" is player's uuid of collection {mongoCollection}
        set mongo value "messages" of {_doc} to {%player's uuid%::messages}
        update mongo document {_doc} of {mongoCollection}

command mymessages:
    trigger:
        set {_doc} to first mongo document with mongosk filter where field "uuid" is player's uuid of collection {mongoCollection}
        set {_myMessages} to mongo value "messages" of {_doc}
        broadcast "%{_myMessages}%"

► This tutorial was brought to you by Skripting Tutorials, check out the channel for more helpful tutorials.


Did you find Adam's tutorial helpful?


You must be logged in to comment

  • Sept. 6, 2022, 8:37 a.m. - skTank  

    Great tutorial useful for those just starting with skript or those that can do skript but haven't touched on Mongo! Thanks :)

    |