Unity3D SDK - Filter query

Hi, since there is no documentation on Unity SDK, I hope someone here could shed a light on how to implement a filter query.

I have the following code:

var client = user.GetMongoClient("mongodb-atlas");
var database = client.GetDatabase("_DatabaseName_");
var collection = database .GetCollection("_CollectionName_");
collection.FindAsync(...)

To create a query, I need to somehow create a filter and pass it to the FindAsync method.
How can I do that, are there any examples for simple queries like LT, GT, EQ …

1 Like

@Yakir_Bukris A Unity tutorial and documentation is in progress! But you can use the .NET documentation, it is all still relevant to Unity -

2 Likes

Hey Yakir, welcome to the forums! :wave:

You can find some C# query examples on the Query MongoDB page of the .NET SDK docs. That should get you the basic syntax you need to write queries. As for specific operators like $lt, $gt, etc the page won’t have detailed reference/examples for each one but you should be able to use the same form as any other MongoDB/language driver as described in the server manual.

e.g.

var client = user.GetMongoClient("mongodb-atlas");
var database = client.GetDatabase("MyApp");
var collection = database .GetCollection("Users");
collection.FindAsync(
  new { name = "Bob" },
  new { _id = 0, name = 1 }
)
2 Likes

Edit: Seems like I was super slow and my post mostly overlaps with Ian and Nick’s. I’ll leave it mostly because it has a code example of how to use BsonDocument.Parse.

Hey, the .NET docs do cover Unity (since it’s the same SDK) and have some minimal querying examples. For the filter, you can either construct a BsonDocument manually, or just use BsonDocument.Parse and pass in pretty much verbatim the json filter like:

var filter = BsonDocument.Parse(@"{
    $and: [
        age: { $gt: 18 },
        name: { $regex: '^pe' }
    ]
}");

That being said, if you’re only looking to query MongoDB, you can probably just use the C# driver instead as it offers a much more comprehensive API to do that. The Unity SDK is primarily intended to be used to read/write data in the Realm mobile database and - optionally - use Sync to upload/download changes from Atlas.

2 Likes

2 posts were split to a new topic: Unity SDK equivalent of .NET builders