影响mongodb _id生成索引 [英] Effect mongodb _id generation on Indexing

查看:133
本文介绍了影响mongodb _id生成索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MonoDB作为数据库.......

I am using MonoDB as a databse.......

我将为
的每个文档生成一个_id,我使用该用户的useId和FolderID

I am going to generate a _id for each document for that i use useId and FolderID for that user

此处每个用户的userId不同,每个用户也有不同的FolderIds

here userId is different for each User and also Each user has different FolderIds

我生成_id为

userId="user1"
folderId="Folder1"

_id = userId+folderId

这个id生成对mongoDB索引有什么影响......
它会像MongoDB生成的_id一样快速运行

is there any effect of this id generation on mongoDB Indexing... will it work Fast like _id generated by MongoDB

推荐答案

更好的解决方案是离开 _id 列原样,并在文档中包含单独的 userId folderId 字段,或创建一个单独的字段,并将它们合并。

A much better solution would be to leave the _id column as it is and have separate userId and folderId fields in your document, or create a separate field with them both combined.

至于它是否一样快......取决于你的查询,但是对于通过创建文档的日期进行排序,例如你将失去只需按 _id 排序,您也会失去分片和分发

As for if it will be "as fast" ... depends on your query, but for ordering by "create" date of the document for example you'd lose the ability to simply order by the _id you'd also lose the benefits for sharding and distribution.

但是,如果您想将这两个ID用于 _id ,还有另外一个选项...

However if you want to use both those ID's for your _id there is one other option ...

您实际上可以使用两个但是将它们分开 ...例如这是有效的 _id

You can actually use both but leave them separate ... for example this is a valid _id:

> var doc = { "_id" : { "userID" : 12345, "folderID" : 5152 }, 
              "field1" : "test", "field2" : "foo" };
> db.crazy.save(doc);
> db.crazy.findOne();
{
        "_id" : {
                "userID" : 12345,
                "folderID" : 5152
        },
        "field1" : "test",
        "field2" : "foo"
}
> 

这篇关于影响mongodb _id生成索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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