Categories
Uncategorized

Copying (duplicating) MongoDB Documents (Rows)

The use case is simple – I have one row (I mean document). I need to make multiple copies of it. Say, I am building a blog app and I need to quickly generate a few posts on the database. Scripting is one way to do it, but if I need to do this only once, automation is a waste of time.

Thanks to mongodb’s shell and JS-like APIs. This is actually simple.

Connect to mongodb:

Select the database:

Find an entry on the “posts” collection:

Now let’s change the “_id”:

X is now a whole new object/document (since we changed the “_id”). We can alter other fields as well. Let’s store it:

You just copied one document into a new document. Cool?

The command line mongo client has readline and history support. So you can actually use the up arrow to repeat the commands quite easily.

Categories
Uncategorized

Vim Madness

I use the top notch IDEs for development but that is not geeky enough. So, ignoring my previous failed attempts, I am going to give the text editor one more try. Apparently, this blog post would be more like self documentation, mostly notes and hints to self. If anyone else also gets benefited, that’d be an added bonus.

Quick Shortcuts

  • Vundle Bundle Installation: “:BundleInstall”
  • New Tab: “:tabnew
  • Navigate Tabs: “:tabn” for next, “:tabp” for prev. “gt” in normal mode
  • Switch Windows: “Ctrl + w, Ctrl+w”
  • Splitting Windows: “:split “, “vsplit” for vertical splits
  • NerdTree – Open in new tab: “t”

Let there be ~/.vimrc

OK, here’s my ~/.vimrc, the initial version is messy, not organized at all, everything I came across, I stuffed in here. I shall update and clean the vimrc over time.

Categories
Uncategorized

Go: Creating new types based on built-in types

Ever needed a custom string type to add your own methods or tweak certain behavior? In languages like Ruby or Javascript, you can directly modify built in types. In other languages, we usually create custom objects to wrap around a default type and then extend that object with necessary methods or attributes.

A sample example in Python would look like:

In Go, we would define a new type based on an existing type. Then define methods on the newly defined type. It allows us to easily create new types based on existing types and then customize to suit our needs. The same example in Go would look like:

Simple, isn’t it?