conditional delete with limit

1,088 views
Skip to first unread message

Aaron Westendorf

unread,
Aug 15, 2011, 4:31:54 PM8/15/11
to mongod...@googlegroups.com
I'm trying to perform a bulk delete using conditions and a limit. The closest I've come is this thread, in which 10gen says it can be done, but doesn't indicate how. I've tried many obvious combinations, via pymongo and the shell, but no matter what I try, 'findAndModify' will only delete one record at a time and it always returns it.  Any pointers on how to get this moving without hacks to manage it on the client side? I'm running 1.8.1.


-Aaron

Sam Millman

unread,
Aug 15, 2011, 4:46:40 PM8/15/11
to mongod...@googlegroups.com
Im not sure if I have ever seen 10gen say that and they dont in that thread cos if you notice the query:

"update jobQ set worker = %s order by insertTime limit 20
select * from jobQ where worker = %s
delete from jobQ where worker = %s"

He is actually doing three different queries there and is not deleting via a limit cluase.

Can you give us examples of this functionality in your code and how you have tried already?

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Scott Hernandez

unread,
Aug 15, 2011, 4:50:32 PM8/15/11
to mongod...@googlegroups.com
The easiest way to do this is probably to do the query to get the _id
values and then to delete them by _id value using $in.

Remove/delete does not have a limit option.

Aaron Westendorf

unread,
Aug 15, 2011, 4:57:57 PM8/15/11
to mongod...@googlegroups.com
I cannot, but I have corrected my reading comprehension and agree that
it does not work as I said. I also used this post to support my
expectation.

http://slacy.com/blog/2010/12/mongodb-doing-a-findandmodify-with-a-limit-in-python/

If mongo does not in fact support this, it should. This is a very
useful feature and the only alternative is to pull in data to the
client, then delete each one.

--
Aaron Westendorf
Senior Software Engineer
Agora Games
359 Broadway
Troy, NY 12180
Phone: 518.268.1000
aa...@agoragames.com
www.agoragames.com

Sam Millman

unread,
Aug 15, 2011, 5:04:07 PM8/15/11
to mongod...@googlegroups.com
I agree it would be a useful feature and you can actually file a JIRA request for this here:

https://jira.mongodb.org/secure/Dashboard.jspa

And 10gen will decide when and how they believe this should be implemented.

Scott Hernandez

unread,
Aug 16, 2011, 1:55:58 AM8/16/11
to mongod...@googlegroups.com
I'm not sure what the discussion is about using limit with
findAndModify is since it makes no sense. If you want to affect more
than one document you should use update and not findAndModify;
findAndModify only affects one document, at most.

Maybe you are talking about adding a limit to update (for the query
part); if so you can vote for this :
https://jira.mongodb.org/browse/SERVER-1599

Sam Millman

unread,
Aug 16, 2011, 3:08:22 AM8/16/11
to mongod...@googlegroups.com
SQL Allows for Limit on delete.

There are very rare cases where some times you wanna delete in certain ways like all the table but but the last row.

But it most likely isn't significant enough for 10gen to worry about in the near future.
Reply all
Reply to author
Forward
0 new messages