Mongodb _id generation effect when indexing

I use MonoDB as a database ........

I am going to create _id for each document for what I use useId and FolderID for this user

here userId is different for each user, and also each user has different FolderIds

i generates _id as

userId="user1" folderId="Folder1" _id = userId+folderId 

is there any effect of this generation id on mongoDB indexing ... will it work fast like the _id created by MongoDB

0
source share
2 answers

The best 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 in which they are combined.

As for whether it will be "just as fast" ... it depends on your request, but for ordering the "date of creation" of the document, for example, you lose the ability to simply order by _id , you also lose the benefits of sharding and distribution .

However, if you want to use both of these identifiers for your _id , there is another option ...

In fact, you can use both options, 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" } > 
+3
source

This should be good - one foreseeable problem is that you lose the ability to cancel the date / timestamp from MongoID. Why not just add another ID object to the document? You only lose a few bytes, and you don’t screw the built-in indexing system.

0
source

Source: https://habr.com/ru/post/1334790/


All Articles