在Web应用程序的MongoDB集合上创建索引的推荐方式/位置 [英] Recommended way/place to create index on MongoDB collection for a web application

查看:55
本文介绍了在Web应用程序的MongoDB集合上创建索引的推荐方式/位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MongoDB用于我们的Web应用程序.假设在MongoDB上将有一个"find()"用于传入请求.在MongoDB集合上添加索引的推荐方式/位置是什么?

I'm using MongoDB for our web application. Assume there will be a 'find()' on MongoDB for incoming requests. What is the recommended way/place to add index on a MongoDB collection ?

我能想到的选项:-

1)在初始化应用程序时在集合上使用"ensureIndex". [但是,如何在应用程序首次初始化时'确保索引'?因为将没有任何数据]

1) 'ensureIndex' on the collection while initializing the application. [But how will I 'ensureindex' at the very first time application initialize ? since there won't be any data in place]

2'ensureIndex'吗?但是,即使'ensureIndex'如果已经创建索引也不会创建索引,这不是开销吗?

2 'ensureIndex' before every 'find' operation (on web request) ? but isn't this an overhead even if 'ensureIndex' wouldn't create index if it is already created ?

还有其他选择吗?

谢谢.

推荐答案

在初始化应用程序时,我会放它.如果在调用sureIndex时不存在该集合,则将在那时创建索引(和集合).

I would put it when you initialize the application. If the collection does not exist when you call ensureIndex, the index (and collection) will be created at that time.

我假设您先验地知道将对数据运行哪种类型的查询,以及将要放入索引中的哪种类型的数据.

I am assuming that you know a priori what kinds of queries you will be running on the data, and what kind of data you will be putting into the index, of course.

这篇关于在Web应用程序的MongoDB集合上创建索引的推荐方式/位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆