Do you know a way to clean-up heroku/mongohq database? - mongodb

I'm looking for a way to reset a mongodb deploied on heroku/mongohq, something like
# heroku rake db:reset
I know that's possible to directly login mongohq and drop it, but I'd like a quick command line.
Thanks in advance
Luca

In your title you say "clean up", in your description you say "reset". You also use the word "drop" as if you want to drop a database.
Generally speaking, you don't want to "reset" MongoDB unless there's some server issue.
You generally don't want MongoDB to "running dirty", especially in such a way that it needs to be "cleaned up".
Generally, dropping a database is either an Admin task or a task associated with some test script you are running
rake is a build tool and these are all functions of DB administration.
Would you be able to be more specific about what you're trying to do?
Alright, so I found some more details. It looks like rake:reset does the following:
:reset => ['db:drop', 'db:create', 'db:schema:load']
So, it does a drop, a create and a schema load. These are all Rails-specific tasks and they're all tied to some specific features of SQL databases.
In general, MongoDB doesn't have a schema, so there's normally no need for db:create and db:schema:load. So basically you're just doing a db:drop (and maybe some ensureIndex commands)?
I'm looking at the rest of these rake tasks, and many of them don't really apply to typical MongoDB use cases. If you really want this functionality, you'll probably have to write some of this for yourself.

Related

Automated functions in Mongo DB

I was wondering if there is something like a script that I can write on the MongoDB side that would do something like delete an item in a list if it is older than a week old etc.
I want to have the DB do this check every day. Are there some kind of automated functions that I can setup on the DB to do this?
I could just write a few small methods to do it on the userside myself, but I remember my old SQL DB having this feature. Any help would be appreciated. Thanks,
If you are using Self-Managed MongoDB, The answer is NO.
Like SQL, MongoDB doesn't support scheduled transactions. However you can run the scheduled jobs in your programming language and perform the operations. For example, https://thecodebarbarian.com/node.js-task-scheduling-with-agenda-and-mongodb
If you are using MongoDB Atlas, Then you need to check this https://docs.mongodb.com/realm/triggers/scheduled-triggers/
Then you might want to check out the MongoDB TTL feature :
https://docs.mongodb.com/manual/tutorial/expire-data/
It won't run every day like a script, but it will automatically remove data after some time.
Hope it can help !

When testing POST (create mongo entries), how to delete entries in DB w/ Jmeter after testing, if you don't have DELETE endpoints?

I'm sure I can write an easy script that simply drops the entire collection from the database but that seems very clumsy as a long term solution.
Currently, we don't have delete endpoints that actually DELETE, we have PUT endpoints that mark the entry as "DONT SHOW/REMOVED" and another "undelete endpoint" that restores the viewing since we technically don't want to delete any data in our implementation of this medical database, for liability purposes.
Does Jmeter have a way where I can make it talk to Mongo and delete? I know there is a deprecated way to talk to mongo via Jmeter but not sure about any modern solutions.
Since I can't add unused code into the repo, does this mean the only solution is for me to make a "extra endpoint" outside of the repo that Jmeter can access to delete each entry?
Seems like a viable solution just not sure if that's the only way to go about it and if I'm missing something.
MongoDB Test Elements were deprecated due to low interest as keeping the MongoDB driver which is being shipped with JMeter up-to-date would require extra effort and the number of users of the MongoDB Test Elements was not that high.
Mailing List Message
Associated JMeter issue
However given you don't test MongoDB per se and plan to use JMeter MongoDB elements only for setup/teardown actions I believe you can go ahead.
You can get MongoDB test elements back by adding the next line to user.properties file:
not_in_menu
This will "unhide" MongoDB Source Config and MongoDB Script elements which you will be able to use for cleaning up the DB. See How to Load Test MongoDB with JMeter for more information, sample queries, tips and tricks.

Is there any MongoDB handler for find event?

I want to know is there any handler or trigger kind of functionality exist in MongoDB which can help me to run my mongo command on find query?
Basically, I want to run specific set of Mongo queries on find query. You can say a listener on find query to run other mongo commands.
Other than that is there anything like create a view from a remote database, I know MongoDB provides creating a view from the same database but I want from the remote database?
I do not want any application level solution like any third party module or watcher script, I want something which is within the MongoDB itself like inbuilt function or any event handling.
I have tried auditing but, it only records the operation and all, does not provide any handler through which I can run my mongo commands like Oracle providing in Fine-grained auditing.
Any help related to this is appreciable, Thanks in advance.

Could I script my monthly postgres maintenance?

I have to perform a monthly maintenance to a postgres database.
I puTTy into the system, navigate to the database and then run 3 commands on 40 different tables:
CLUSTER [table1] USING [primarykey];
ANALYZE [table1];
REINDEX TABLE [table1];
I have to wait for each command to finish executing before I can run the next one (i.e. CLUSTER, -wait up to a few minutes-, ANALYZE -wait-, REINDEX -wait-, )
It's very simple to do but it takes around 30-45 minutes of me just copying and pasting 120 lines, one line at a time... is there any way to automate this process?
I have zero experience with scripting and I know very little about postgreSQL.
My question is somewhat unique because I cannot install anything in the postgreSQL database. I want to have this script localized on my computer and then be able to run it when it's time for the maintenance.
Clustering automatically reindexes the table. There is no reason to reindex the table immediately after you cluster it.
Do you actually need to do this stuff? Do you have evidence that your tables are in need of clustering? Or you just assuming they do because of something you read off the internet referring to a decade-old version of PostgreSQL written by someone who didn't know what they were talking about in the first place? It is possible you really would benefit from this. It is even more possible you wouldn't, and it is just a waste of time.
If you know nothing about scripting, then you need to learn something about scripting. You should probably tag your post as being about scripting, in whichever shell/language you would like to use.
At the core, all you have to do is write a series of commands to be executed from the command line, and shove them into a text file. The easiest way is probably to install psql on your local computer, if it is not already there.
psql -c 'cluster foobar' -h thehost.example.com
psql -c 'analyze foobar' -h thehost.example.com
You might need to do some configuration to make this connection work with whatever authentication method you have in place, but without knowing which authentication method that is I can't comment further.
If the cluster for some reason fails, there is little reason to proceed to try to analyze it. (But there is also little harm in doing so). If you want to fine tune this situation, there are a variety of ways to do it, depending on which shell you are writing your script for, and what you want it to do.

How to clone a mongodb collection programmatically through Mongo Java driver of Spring Data MongoDB?

I want to clone a collection to a backup collection, before processing all the entries in it through my Spring controller.
On searching, I came to know about various ways to do so through terminal. There is also a possibility to use executeCommand menthod to directly execute mongodb commands (copyTo command in my case). But again, I could not find a way to do so.
Could somebody please provide me any pointers on this?
you can setup JMX support to your project: http://static.springsource.org/spring-data/data-document/docs/current/reference/html/ , see "Chapter 10. JMX support" section, you will be able to get an administrative MBean, MongoAdmin which will let you perform administrative operations such as drop or create a database, and execute one of your solution candidates, like execute mongodump(http://docs.mongodb.org/manual/reference/mongodump/#cmdoption-mongodump--collection) and mongorestore(http://docs.mongodb.org/manual/reference/mongorestore/#cmdoption-mongorestore--collection) for a simgle collection, during restoration you can indicate the new collection name.
Regards,
Moacy