Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 我应该确保每个插件都有索引吗?_Node.js_Mongodb_Indexing - Fatal编程技术网

Node.js 我应该确保每个插件都有索引吗?

Node.js 我应该确保每个插件都有索引吗?,node.js,mongodb,indexing,Node.js,Mongodb,Indexing,我设置了一个唯一的索引,这样我就不会插入重复的订单。我担心,如果我以后将数据库切换到生产环境中,它们可能会通过?我应该这样离开吗?我知道这很愚蠢,因为一旦它在索引第一次存在时运行。这是一项明智的投资,还是会降低我的代码速度 db.orders.ensureIndex( { "marker": 1 }, { unique: true } , function(err, val){ db.orders.insert(orders); }); 您只能ensureIndex一次,以后的时间将被

我设置了一个唯一的索引,这样我就不会插入重复的订单。我担心,如果我以后将数据库切换到生产环境中,它们可能会通过?我应该这样离开吗?我知道这很愚蠢,因为一旦它在索引第一次存在时运行。这是一项明智的投资,还是会降低我的代码速度

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

您只能
ensureIndex
一次,以后的时间将被注册为无操作,因此对于许多设置来说,这不会有任何区别

但是,只使用
ensureIndex()
一次,编码看起来更整洁(不仅可读性更强)。另外,正如我在前面关于这个主题的一个问题中所回答的(更详细),如果要更改索引,而您要将其备份到分片副本集或之前没有索引(或者如果索引已更改)的同样复杂的对象上,那么您可以很容易地关闭数据库集群

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.