Should I ensureIndex on each insert?

I setup a unique index so that I don't insert duplicate orders I'm afraid that they might go through if I switch the database later on to production? Should I leave this like this? I know it's stupid because once it runs the first time the index exists. It it a smart investment or going to slow down my code?

db.orders.ensureIndex( { "marker": 1 }, { unique: true } , function(err, val){
    db.orders.insert(orders);
});

You can only ensureIndex once, further times will be registered as a no-op so it doesn't make any difference for quite a few setups.

However, it looks tidier in your coding (not only more readable) to only ensureIndex() once. To add, as I answered in a previous question on this subject (in more detail), if an index were to change and you were backing that onto a sharded replica set or something just as complicated which does not previously have the index (or if the index has changed) then you could bring your database cluster down quite easily.

Instead of relying on ensureIndex as part of the query , why not set for once a unique index on the collection itself. And MongoDB will take care of ensuring uniqueness.

Uniqueness of a field is a design decision and should be enforced at the time of db setup.

Have a separate script for DB setup and run it for configuring the db.