This article is going to illustrate how to connect MongoDB with unity. Besides, I will write another article about how to connect MYSQL with unity as well.
I have put all the material and source code on Github, here is the link: (https://github.com/Hanslen/unityWithMongodb/tree/master)
Import packages:
First you need to import the unity+mongodb.unitypackage, which is in the Github link. You can download, it contains “MongoDB.Bson.dll” and “MongoDB.Driver.dll”.
Writing script to connect with MongoDB:
Firstly, I create an empty object and link it with a script. In that script, you need to include these
These you ensure you can use that packages, then, modify the main class. For this example, I write like these:
|
|
The connectionString stands for the mongodb host, you can check your host by looking at your ./mongod(for mac) or mongod.exe(for windows):
Query collections:
To get all the collections, you can use:
If you just want to find one accurate row, use:
If you want to select the first row, use:
If you want to select the column where name = “nike”, use:
Insert into Collections:
The following code is a sample with inserting several lines of data, if you want to insert one line, it’s the same, code:
There are some more advanced querying data code, which will be in the file advanced.cs
. You can check that out. Enjoy your unity journey with MongoDB. Thanks for reading. 🙂
Many thanks for fabifiess who shared his code on GitHub.