databasemongodbadhoc-queries

Are all Queries in MongoDB ad-hoc?


Sorry, maybe the question is stupid, but I can't figure it out. Are all queries in MongoDB ad-hoc? Or ad-hoc queries can be executed in special cases?


Solution

  • It is my understanding that, at the most basic of levels, an ad-hoc query allows for the developer to provide variables into the query. Meaning, the full query is only known at the time of execution.

    Meaning, not all queries are ad-hoc, but MongoDB does support ad-hoc queries.

    An example of an ad-hoc query in Mongo would be something like:

    // this example uses node.js
    const results = await db.collection.find({ name: req.query.name });
    

    In the above example, req.query.name is only known at the time of execution, thus making our query an ad-hoc query.

    Please let me know if you have any questions.