8

Does MongoDb have anything like MySql's SELECT SLEEP(5); ?

I can see some internal sleep function that would pause the whole server, but I need to pause just the current query.

Disclaimer: just for testing purposes

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • Are you trying to simulate a slow query? – Gabe Oct 16 '12 at 06:01
  • I don't think such a thing exists atm, at least I have never heard of it. I suppose you could use a `$where` with some sneaky coding to make the JS thread sleep, but only for testing of course. However this will induce a sleep or `timeout()` per doc iteration of the find. – Sammaye Oct 16 '12 at 07:38

4 Answers4

8

You can use the $where operator to call sleep(). This should work in any language or ORM/ODM. For example, in Mongoid you could do:

Model.where( :$where => "sleep(100) || true" ).count

Tune the sleep value for the number of documents in the collection (it will delay on each one). This will do fairly horrible things to the DB server, so only use it for testing, and never (ever!) on a production server.

3

You can use the $where operator as code_monkey_steve says, but be sure you do with mongo version >= 2.4. Before that version no javascript can be run on parallel on the same server. The sleep command is javascript, so it would apparently block other javascript queries you could be making.

Community
  • 1
  • 1
RubenCaro
  • 1,419
  • 14
  • 12
2

from mongo Shell you can run :

db.collection_name.find( { $where: function() { return sleep(100) || true }}).pretty();

tested and working.

1

From the mongo shell you can do sleep( ms ), eg sleep for 5 seconds before running a query:

> sleep(5000); db.collection.find(..);

This doesn't pause the current query, but does pause execution on that connection for the specific number of milliseconds before continuing with the next statement (which is equivalent to a select sleep(5) in MySQL).

Stennie
  • 63,885
  • 14
  • 149
  • 175