简体   繁体   English

影响mongodb _id生成对索引的影响

[英]Effect mongodb _id generation on Indexing

I am using MonoDB as a databse....... 我正在使用MonoDB作为数据库.......

I am going to generate a _id for each document for that i use useId and FolderID for that user 我将为该用户使用useId和FolderID的每个文档生成一个_id

here userId is different for each User and also Each user has different FolderIds 这里的userId对于每个用户都是不同的,并且每个用户都有不同的FolderIds

i generate _id as 我生成_id为

userId="user1"
folderId="Folder1"

_id = userId+folderId

is there any effect of this id generation on mongoDB Indexing... will it work Fast like _id generated by MongoDB 这个id生成对mongoDB索引有什么影响...它将像MongoDB生成的_id一样快速地工作

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不变 ,并在文档中具有单独的userIdfolderId字段,或者创建将它们结合在一起的单独字段。

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进行排序的能力,也将失去收益分片和分发

However if you want to use both those ID's for your _id there is one other option ... 但是,如果您想将这两个ID都用于_id ,则还有另一种选择...

You can actually use both but leave them separate ... for example this is a valid _id : 您实际上可以同时使用两者,但是将它们分开 ...例如,这是有效的_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"
}
> 

It should be fine - the one foreseeable issue is that you'll lose the ability to reverse out the date / timestamp from the MongoID. 没关系-一个可以预见的问题是,您将失去从MongoID撤消日期/时间戳的功能。 Why not just add another ID object within the document? 为什么不只是在文档中添加另一个ID对象呢? You're only losing a few bytes, and you're not screwing with the built in indexing system. 您只丢失了几个字节,也没有使用内置的索引系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM