简体   繁体   中英

Effect mongodb _id generation on Indexing

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

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

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

i generate _id as

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

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.

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 .

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

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"
}
> 

It should be fine - the one foreseeable issue is that you'll lose the ability to reverse out the date / timestamp from the MongoID. Why not just add another ID object within the document? You're only losing a few bytes, and you're not screwing with the built in indexing system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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