Help, Mongo insert only if total document count is less than N, Concurrent conditional inserts and updates

I want to insert a document into mongo only if the total count returned by my query is less than a certain threshold number N. Is this kind of query possible in mongodb or should I switch to relational databases where this can be handled with case and where constraints in the query.

I am looking for a solution where these kind of insertions would happen at high scale and concurrency, and still the insertion threshold should not get exceeded.

I could find this kind of solution in relational databases, also doing a POC with SP(Stored Procedures) to find if this can be handled.

Thanks in Advance.