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?
Asked
Active
Viewed 1,326 times
2
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 07 '21 at 08:42
1 Answers
3
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.

Dharman
- 30,962
- 25
- 85
- 135

Matt Oestreich
- 8,219
- 3
- 16
- 41
-
I am not sure we have the same definition of "ad hoc query" ; your example looks more like a prepared query, which awaits a value for the search parameter. "Ad hoc queries" are often meant as "queries we had not thought about when designing the data-model, or the data access-pattern". This is typically BI queries. Are both significations co-existing, or is one of us wrong? – galeop Nov 04 '22 at 13:23
-
@galeop it is very possible my understanding is incorrect. If you scroll all the way down [here](https://hashnode.com/post/what-are-ad-hoc-queries-cj16d0k65000ig753yqy152aj) it shows an example of what I thought was an ad-hoc query. See [here](https://stackoverflow.com/a/2460987/10431732) as well. If I am wrong I would love to know so I can update my answer! Thanks for pointing this out and mentioning your concerns. – Matt Oestreich Nov 04 '22 at 14:53