Search code examples
c#mongodbunity-game-enginebson

Is there a way to make the Limit value interactive in MongoDB c#?


I am wondering if there is a way to make the Limit() variable changeable when the application is running? Here is my code:

    void Start()
    {
        m_ButtonRun.onClick.AddListener(TaskOnClick);
    }
    public void TaskOnClick() //
    {
        string connectionString = "myconnection";

        var client = new MongoClient(connectionString);
        var database = client.GetDatabase("mydb");
        //var database = server.GetDatabase("WIVEmydbData");
        var collection = database.GetCollection<BsonDocument>("mycollection");
        var sort = Builders<BsonDocument>.Sort.Descending("Time");

        var document = collection.Find(new BsonDocument()).Sort(sort).Limit(limit: 1).ForEachAsync(d => Console.WriteLine(d));
        Console.WriteLine(document.ToString());

        //streamwriter writes the Console.WriteLine to multi-import.txt
        FileStream filestream = new FileStream("import.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        var writeFile = new StreamWriter(filestream);
        {
            writeFile.AutoFlush = true;
            Console.SetOut(writeFile);
            writeFile.Write(document.ToString());
        }
}

Is it possible to assign a public Button to increasing the limit by 1 for example somehow? My idea is to have 3 buttons: "increase limit", "execute the DB search", "decrease Limit". The execute part works perfectly, just wondering if it's possible at all to make the Limit() customized on the fly.

Edit: Added the complete code... oh boy What I thought about was to put something like an X in the () of the Limit(), set the X to be a default of 1 and then have two buttons to add or subtract 1 from the X value.


Solution

  • As stated in the comments I'm pretty sure you could simply do something like

    [SerializeField] Button increaseButton;
    [SerializeField] Button decreaseButton;
    
    private void Start()
    {
        m_ButtonRun.onClick.AddListener(TaskOnClick);
        increaseButton.onClick.AddListener(IncreaseLimit);
        decreaseButton.onClick.AddListener(DecreaseLimit);
    }
    
    private int limit = 1;
    
    public void IncreaseLimit()
    {
        limit++;
    }
    
    public void DecreaseLimit()
    {
        limit--;
    
        limit = Mathf.Max(limit, 1);
    }
    
    public void TaskOnClick() //
    {
        string connectionString = "myconnection";
    
        var client = new MongoClient(connectionString);
        var database = client.GetDatabase("mydb");
        //var database = server.GetDatabase("WIVEmydbData");
        var collection = database.GetCollection<BsonDocument>("mycollection");
        var sort = Builders<BsonDocument>.Sort.Descending("Time");
    
        var document = collection.Find(new BsonDocument()).Sort(sort).Limit(limit).ForEachAsync(d => Console.WriteLine(d));
        Console.WriteLine(document.ToString());
    
        //streamwriter writes the Console.WriteLine to multi-import.txt
        FileStream filestream = new FileStream("import.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        var writeFile = new StreamWriter(filestream);
        {
            writeFile.AutoFlush = true;
            Console.SetOut(writeFile);
            writeFile.Write(document.ToString());
        }
    }