Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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
mongodb索引名_Mongodb - Fatal编程技术网

mongodb索引名

mongodb索引名,mongodb,Mongodb,我使用java代码创建了mongo db集合索引 dbCollection.createIndex("accountNumber"); 当我看到索引使用 db.accounts.getIndexes() 我得到的索引名是“accountNumber\u 1” 如何获得与文档字段相同的索引名?或者如何给出索引名 命名索引重要吗?或者我可以忽略这一点?当我们在文档用户上创建索引时 > db.users.createIndex({name: 1}) { "ok" : 0,

我使用java代码创建了mongo db集合索引

dbCollection.createIndex("accountNumber");
当我看到索引使用

db.accounts.getIndexes()
我得到的索引名是“accountNumber\u 1”

如何获得与文档字段相同的索引名?或者如何给出索引名


命名索引重要吗?或者我可以忽略这一点?

当我们在文档
用户上创建索引时

> db.users.createIndex({name: 1})
{
        "ok" : 0,
        "errmsg" : "Index with name: name_1 already exists with different option
s",
        "code" : 85
}
返回
name:name_1
,然后我们可以通过
getIndexes()


我们知道,
name_1
只是index
name
的值。键
name
用于为文档
用户创建索引。我认为
name_1
name
的值,以满足
BSON
结构。我们可以忽略它…

您可以使用createIndex方法的另一个变体,参考java API,使用您想要的名称创建索引


public void createIndex(DBObject键,
数据库对象选项)
在指定的字段上创建索引(如果该索引不存在)。
在MongoDB 3.0之前,dropDups选项可以与唯一索引一起使用,允许在构建索引时删除具有重复值的文档。MongoDB的更高版本将自动忽略此设置

参数: 键-包含与要索引的字段名称和索引顺序成对的文档 选项-控制索引创建的文档。 MongoDB文档 索引创建教程

您可以使用相应的mongodb文档


基本上,第二个参数“options”包含一个显式提供索引名的选项。

您需要使用索引名的具体原因是什么?当您
hint
to mongo在查询中使用哪个索引时,您可以指定索引的字段名,而不是实际的索引名:
db.users.find().hint({age:1})
您可以忽略此项。这个名字无关紧要
> db.users.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.users"
        },
        {
                "v" : 1,
                "unique" : true,
                "key" : {
                        "name" : 1
                },
                "name" : "name_1",
                "ns" : "test.users",
                "background" : true,
                "safe" : null
        }
]

public void createIndex(DBObject keys,
                        DBObject options)
Creates an index on the field specified, if that index does not already exist.
Prior to MongoDB 3.0 the dropDups option could be used with unique indexes allowing documents with duplicate values to be dropped when building the index. Later versions of MongoDB will silently ignore this setting.

Parameters: keys - a document that contains pairs with the name of the field or fields to index and order of the index options - a document that controls the creation of the index. MongoDB documentation Index Creation Tutorials