#MongoDB + Unity (Realms)

1 messages · Page 1 of 1 (latest)

wind glade
#

@worldly fox

#

Unity is saying i should use PartitionSyncConfiguration instead of SyncConfiguration(user);

#

but in the MongoDB website it says its deprecated

worldly fox
#

should work fine

#

btw Unity has its own system now with each player stores their own data, also comes with Auth

wind glade
#

but i need the information in the web

worldly fox
#

the unity system is cloud

wind glade
#

the problem is that i already have a DB in MongoDB xD

#

im trying to access it

worldly fox
#

yeah but you still wouldn't want to store your connection string inside of Unity

#

if someone decompiles your code they will have it

wind glade
#

hmm bet

#

yeah i dont want that xD

#

let me show u what i got

#
using UnityEngine;
using Realms.Sync;
using Realms;

public class MongoDBManager : MonoBehaviour
{
    private async void Start()
    {
        // Configuração da instância Realm
        var app = App.Create("application-id");

        // Logue com as credenciais do MongoDB Realm
        var credentials = Credentials.EmailPassword("mail", "pass");
        var user = await app.LogInAsync(credentials);

        // Abra uma conexão com o Realm
        var configuration = new SyncConfiguration(user);
        var realm = await Realm.GetInstanceAsync(configuration);

        // Obter a coleção
        var collection = realm.GetCollection<Users>("users");

        // Exemplo de operações com a coleção
        Debug.Log(collection);
        /*
        realm.Write(() =>
        {
            collection.Add(new MyModel { Name = "Example" });
        });

        // Consulta de dados
        var result = collection.Where(o => o.Name == "Example");

        foreach (var item in result)
        {
            Debug.Log("Item: " + item.Name);
        }
        */
    }
}


public class Users : RealmObject
{
    [PrimaryKey]
    public string _id { get; set; }

    public int Advertencias { get; set; }
    public int ApostarParaResgato { get; set; }
}
worldly fox
#

yeah so just use realm

wind glade
#

So u say i can still use partitionSync right?

worldly fox
#

yea should still work fine

#

I still use it

wind glade
#

how did u made the connection?

worldly fox
#

was going to look into the flexible mode but haven't gotten around to it

wind glade
#

i thought i didnt needed the email and password

worldly fox
#

with AppID

wind glade
worldly fox
#

pretty much thats why the SDK is there

wind glade
#

thx imma look it up 🙏

worldly fox
#

also you want device Sync if you have an actual mongodb Atlas to sync to instead of using realm only

wind glade
worldly fox
#

thats what the Device Sync feature is for

#

Realm was technically a mobile devices db

#

then got expanded and bought into mongo

#

now got renamed to atlas sdk though so haven't kept up with that yet

#

been working on Unity's cloud system :p

wind glade
#

ahh ok

#

are u using LogInAsync()?

worldly fox
#

anything to do with web i do async yes

wind glade
#

I cant connect just using the realm app id

#

in the MongoDB website it gives this example: ```c#
var app = App.Create("application-id");
var credential = Credentials.Anonymous();
try
{
await app.LogInAsync(credential);
}
catch (Exception ex)
{
Console.WriteLine($"Login failed: {ex.Message}");
}

#

but Unity is refusing to accept Anonymous or local-userpass credentials

worldly fox
#

are you getting error ?

#

did you activate auth inside Mongo

wind glade
#

im not sure

wind glade
#

Login failed: InvalidSession: authentication via 'local-userpass' is unsupported

#

and it says the same thing using anonymous

worldly fox
#

under Authentication ?

wind glade
#

should i use API-key?

#

it worked im logged in but im getting this fat error: ```c#
Connection[1]: Session[1]: Received: ERROR "Invalid schema change (UPLOAD): non-breaking schema change: adding schema for Realm table "Users", schema changes from clients are restricted when developer mode is disabled" (error_code=225, try_again=false, recovery_disabled=false)

#

imma try enable dev mode

worldly fox
#

yup

#

also dealing with scehem/breaking changes starts becoming a pain in the ass

#

make sure you research that

wind glade
wind glade
#

I think i know the problem, tell me something.

#

How does the app know which user to connect in the database? Because my database has no reference to unity ids.

wind glade
#

idk xD

#

im using discord ids

#

how to search for a specific user?

worldly fox
#

which user are you talking about?

wind glade
#

no, i meant the database documents

#

_id

#
2024-05-02 19:58:26.976 Error: Connection[1]: Session[1]: Failed to integrate downloaded changesets: Failed to transform received changeset: Schema mismatch: 'user' has primary key '_id', which is nullable on one side, but not the other
worldly fox
#

yes the schemas have to match

wind glade
#

i used the schema made by MongoDB

#

look

worldly fox
#

!code

fluid whaleBOT
wind glade
worldly fox
#

so on this class ID is not nullable but apparently is on the schema

wind glade
#

Using IRealmObject i get this errors

#

can _id be nullable? How?

worldly fox
#

you dont know how to make it nullable where did you get this class from?

wind glade
#

from MongoDB website

worldly fox
#

where

#

also why is everything crammed into one class

wind glade
wind glade
wind glade
worldly fox
# wind glade

alright well somewhere then you mistmatched the nullable

wind glade
#

bet

wind glade
worldly fox
# wind glade

should be fine if the class is partial, its been a while since I touched this

#

make sure it was saved properly

wind glade
#

what the code?

worldly fox
#

yeah the class

wind glade
#

i got auto-save 😦

wind glade
worldly fox
#

normally interfaces force you to implement methods inside

#

but the partial class should be what makes it so you don't have to do it in this object

wind glade
#
var items = _realm.All<users>();
Debug.Log(items); //Realms.RealmResults`1[users]
#

im getting this, but i have two documents. Cant figure it out. 😦

worldly fox
#

maybe they're not in sync properly

#

not sure

wind glade
#

how can i check that?

worldly fox
#

try inserting document then check online ?

wind glade
#

0 Sync Sessions, does that mean its not working?

worldly fox
worldly fox
wind glade
#

it turns to 1 if i play in unity

#

so it should be working... right? Im installing the Realm like u sad.

#

I downloaded the realm-dotnet-12.1.0 and the Realm Studio. But it gives me this messages when i try login.

#

Im sure im using the right APP-ID and API-Key

wind glade
#

And using _realm.All<users>().Count(); I get 0

wind glade
#

I saw on internet peaple saying that there is a problem with IRealmObject and that its possible to use RealmObject

worldly fox
#

I'm not sure . Like I said I've been since using the Unity built in database solution

#

all these are new errors to me

wind glade
#

hey @worldly fox I have been trying different stuff.

#

Realm is working but i think its only locally. I can create and find objects from Realm but nothing on the MongoDB Clound is detected or changed.

#

Its only working on my PC i think. How can i change it?

wind glade
#

Its working!

worldly fox
worldly fox
wind glade
#

Yeah

#

Thank u very much 🙏

#

For all the help