MongoDB Removal Status

20 views
Skip to first unread message

John Reed

unread,
Jul 1, 2015, 9:46:02 PM7/1/15
to mongod...@googlegroups.com
Hello,

I have been tasked with writing a BASH script that cleans out records from a MongoDB collection as well as other things. The script is a cleanup script that will run from a cron job nightly in order to prevent certain records from filling up disk space. Unfortunately for me, I'm a little new to MongoDB- so I am dealing with a bit of a learning curve here.

For the parts of this script that are relevant to my MongoDB question... The BASH script I have written gathers a bunch of UIDs from another source and writes out a mongoscript.js file with a bunch of remove statements like this:

db.events.remove( { "UID": "xxxxxxxxx" } )
db.events.remove( { "UID": "xxxxxxxxx" } )
db.events.remove( { "UID": "xxxxxxxxx" } )
db.events.remove( { "UID": "xxxxxxxxx" } )

Then the next part of the script, it runs the mongoscript.js in a one-liner using mongo:

mongo $MONGODB --host $MONGODB_HOST --port $MONGODB_PORT --username $MONGO_USER --password $MONGO_PASS mongoscript.js

This all works great and it removes all the records I am expecting to remove. But I'm not satisfied with whats going on here...

The problem I have is that I cannot error-check this because the db.<collection_name>.remove( { some json } ) method doesn't return anything. Even when I run these commands manually from the Mongo Shell, there are no return codes or returned json telling me how many records were deleted or if something failed, etc. Does anyone know how I can produce the same results but with some error checking so I can validate that these entries were in fact removed?

Many Thanks!

John



John Reed

unread,
Jul 2, 2015, 8:50:34 AM7/2/15
to mongod...@googlegroups.com
So looking further into the issue, the MongoDB instances I am working with seem to not return the "WriteResult()" when you remove objects, which is why I am not seeing anything when doing a db.events.remove( <json> ). I am assuming this is not a default behavior as when installing MongoDB on my laptop, I always get a WriteResult() returned when inserting or removing records. Does anyone know why MongoDB would not be returning WriteResult()? Is there a way that this could be turned off and maybe I just need to turn it back on?

Stephen Steneker

unread,
Jul 2, 2015, 9:03:32 AM7/2/15
to mongod...@googlegroups.com, jo...@spindance.com
On Thursday, 2 July 2015 22:50:34 UTC+10, John Reed wrote:
So looking further into the issue, the MongoDB instances I am working with seem to not return the "WriteResult()" when you remove objects, which is why I am not seeing anything when doing a db.events.remove( <json> ). I am assuming this is not a default behavior as when installing MongoDB on my laptop, I always get a WriteResult() returned when inserting or removing records. Does anyone know why MongoDB would not be returning WriteResult()? Is there a way that this could be turned off and maybe I just need to turn it back on?

Hi John,

What version of MongoDB are the remote servers running? I suspect you're testing locally against MongoDB 2.6+ and the remote server is running something older than 2.6. WriteResult is part of the newer write operations protocol introduced in MongoDB 2.6:

Older versions of MongoDB required a separate getLastError call to get details of the preceding write command on the current connection:

Regards,
Stephen

John Reed

unread,
Jul 2, 2015, 9:06:49 AM7/2/15
to mongod...@googlegroups.com, jo...@spindance.com
Stephen,

I think you hit the nail on the head. It looks like I'm using an older version on the server:

> db.version()
2.4.9

I'll have to experiment with the dbcmd.getLastError()

Thanks for the reply!

John
Reply all
Reply to author
Forward
0 new messages