MongoStore - An alternative to DataStores

MongoStore

An alternative to DataStoreService, made with Rongo
v1.0.0

:warning: MongoStore is using an outdated version of Rongo, update coming soon :warning:


Download on Roblox :inbox_tray:
Download on Github :link:
View Source :computer:


What is MongoStore?

MongoStore is an open-source alternative to Roblox’s DataStoreService.

It makes use of MongoDB in order to save player data (using Rongo as an interface)

MongoStore is currently quite limited in features but it includes the “main” function of Roblox’s DataStoreService, which is :GetDataStore(). When you run this, you’ll get an object back which behaves similarly to Roblox’s GlobalDataStore, although MongoStore lacks a couple functions from Roblox’s one.

MongoStore aims to make the transition between DataStoreService and MongoDB as smooth as possible, which is why it behaves almost exactly the same.

MongoStore is licensed under the MIT License

How do I install MongoStore?

If you’d like to install MongoStore, then either grab the model from the Roblox Library or download the file on Github!

Once you’ve gotten either of those, insert it into ServerScriptService & then you’re ready to start using it!

Scroll down for some usage examples & documentation :slight_smile:


Examples

This requires basic knowledge of MongoDB

Setting up the Data API on MongoDB

Follow the steps below to get MongoDB’s Data API Setup

1 - Click on the “Data API” page in MongoDB
image

2 - Enable the Data API
image
(make sure to select the sources)

3 - Copy your API ID & store it somewhere safe
image
image
(only copy this, don’t copy the entire URL)

4 - Click on the “Create API Key” button
image

5 - Enter a name then press the generate button
image

Copy your API key & store it somewhere safe
image

Once you’ve done all these steps you should be good to go and you’re ready to setup MongoStore in your game!

Using MongoStore in your game

After you’ve set up the Data API & installed MongoStore in your game, you can start using it. Below will teach you the basics of getting it set up!

1 - Create a database & collection on MongoDB
This can be done by clicking on the “Collections” tab in your MongoDB Cluster & then pressing the “Create Database” button
image

This is what I’ll be calling my database & collection, you can call them whatever you want though.
image

2 - Create a script in ServerScriptService for MongoStore

3 - Open the script & replace the content with the script below

local ServerScriptService = game:GetService("ServerScriptService")
local MongoStore = require(ServerScriptService.MongoStore) --// Replace this with the location where you placed the MongoStore module

MongoStore:Authorize(APP_ID,APP_KEY) --// Replace these with your App ID & API Key (This is required to use MongoStore)

local DataStore = MongoStore:GetDataStore("DataStore","PlayerData") --// Replace these with your database name & your collection name
--// You can also change the cluster name from the default (Cluster0) by adding an extra argument with your cluster name

This is a basic script which will let you start using MongoStore!

Simple player data script

This example will show you how to create a script which will set & update a players data when they join & leave the game!

1 - Below the base script that you made in the previous example, paste this code:

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local success,data = pcall(function()
		local Result = DataStore:GetAsync("_"..Player.UserId)
		return Result
	end)

	if not success or not data then
		pcall(function()
			DataStore:SetAsync("_"..Player.UserId,{["gold"] = 0})
		end)
		data = {["gold"] = 0}
	end
	
        print("Your gold:",data.gold)
	Player:SetAttribute("Gold",data["gold"] or 0)
	
	task.spawn(function()
		while task.wait(1) do
			Player:SetAttribute("Gold",Player:GetAttribute("Gold")+1)
		end
	end)
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player)
	pcall(function()
		DataStore:UpdateAsync("_"..Player.UserId,{["gold"] = Player:GetAttribute("Gold")})
	end)
end)

This script will fetch the players “gold” when they join the game & then will give the player one gold every second until the player leaves. Once the player leaves, it’ll save the players gold!

2 - Test the script!
Open the game in Roblox Studio & look at output, it should print out the gold you have when you join!

Wait a few seconds then leave the game & rejoin then look at output again, you should see your new gold!

You can also look at your collection on MongoDB to see your gold or other players gold:
image

3 - Modify the script to your needs
You can now change the script around or remake it to fit your needs!


Documentation

Classes

DataStore

Functions

Module

function MongoStore:Authorize(AppId: string,AppKey: string) -> boolean
   AppId: The ID inside your Data API URL
   AppiKey: The API Key which you generated in MongoDB

-- You only need to authorize in a single script --
function MongoStore:GetDataStore(Name: string,Scope: string,Cluster: string?) -> DataStore
   Name: The name of your database/datastore
   Scope: The name of your collection
   Cluster: The name of your cluster (default: Cluster0)

DataStore

function DataStore:GetAsync(Key: string): {[string]: any?}?
   Key: The key for the datastore you want to get
function DataStore:UpdateAsync(Key: string,Value: any): boolean
   Key: The key for the datastore you want to update
   Value: The updated value of the datastore
function DataStore:SetAsync(Key: string,Value: any): boolean
   Key: The key for the datastore you want to set
   Value: The new value of the datastore
function DataStore:RemoveAsync(Key: string): boolean
   Key: The key for the datastore you want to remove

Credits

MongoStore was developed by @Starnamics using Rongo & was made possible by MongoDB

Feel free to use MongoStore in whatever way you want as long as it follows the license terms

Thank you! :slight_smile:

22 Likes

Question, (not to be rude and I love to try it out!)
Why should we use this module if we have datastore, and I agree its because of data loss.
But we still have DataStore2 and ProfileService.

And since this module get/post requests from external sites then we have to enable HttpRequest, and what if someone’s game is backdoored and secretly hidden.
Enabling HttpRequest can make a backdoor send your game to their discord server by using discord’s webhook.

And how fast is your module? how fast it get requests?
Also MongoDB have 3 TB+ storage each node right?

Well I appreciate you spent time for this community :heart:
No hate or anything, its just a question.

Thank you!

3 Likes

This is very well made and can be used, though I really can’t see why we shouldn’t just use the normal DataStores or DataStore2?

I appreciate this module for what it is, as I’m someone who’s quite fond of MongoDB.
However, I can’t see a good reason to use this in production.

Having your game be heavily reliant on HttpService is never a good idea from my experience. This is partially due to rate limiting, but also because storing all of your player (or other important) data on an external web server will require you to pay for hosting for as long as you plan to keep your game working.

MongoDB can of course be self-hosted, but I don’t see a reason to do this when you have Roblox DataStores which will be running 24/7. Data loss/other issues with them are few and far between, and there also libraries (as mentioned above) that mitigate this.

1 Like

Roblox datastores exist solely for the purpose of pulling and pushing data within the same experience. This solution allows you to create cross-experience datastores or use it to create other external projects.

5 Likes

Well, this module would make it easier to externally manage you data using the vast array of MongoDB drivers & like you said, data loss.

Yeah I do understand the worry over the security of using HTTP Requests but people should also ensure that the scripts they’re using are safe to use before putting them into their games.

I’ll need to benchmark this but in testing, it seemed to be pretty fast, in around the 1-3 second range (on their free tier). I’ll edit this once I’ve benchmarked the response time

I’m not entirely sure but if you’re worried about storage, I believe you should be fine, even with huge games which have hundreds of thousands of players, you’ll only be using around ~10GB of data (just an assumption though)

1 Like

This module is made for people who want to use an external database instead of using Roblox’s DataStores

Well, let’s say you have a group “roleplay” game which has ranks in game & you want to be able to modify these ranks without needing to open studio or join the game, you can use this module instead of DataStore service which would give you the ability to modify them externally without ever needing to touch Roblox.

It is also more reliable than DataStore service in terms of data loss.

For the rate limiting part, you shouldn’t really encounter an issue with this but if people do start getting rate limited using my module, I’ll implement a feature for a queue system, which it currently does not have.

For the web server part, if people don’t want to pay then MongoDB provides a free tier which does not require anything other than an account.

Adding on to my previous replies, like @Kostiskat mentioned, external databases also allow you to do cross-experience datastores and with this, you don’t need to worry about purchasing a server to host your database or even worry about purchasing anything if you are fine with MongoDB’s free plan.

Roblox DataStores are good as well, especially when using things like DataStore2 & ProfileService, but for those who want the vanilla DataStore experience but with an external DB without needing to worry about the restrictions of DataStores, MongoStore is a good solution.

1 Like

Alright thanks! I’ll definitely use this module in my projects :smile:

1 Like

not just for cross server database. but you can also view all data keys without having to limit the key to number only. i hatr that limit

1 Like

What are the benefits of using an external database rather than roblox’s built in DataStore?

Cross platform datastores, cross experience datastores, less data loss, almost no limitations, ability to change data without needing to touch Roblox.

1 Like

Does have support for self-hosted instance of mongoDB?

1 Like

I am not entirely sure. I do not believe so though. I’ll look into it more soon