How are CouchDB Document ID's Calculated? - hash

How are the CouchDB Document ID's calculated?
BA1F48C5418E4E68E5183D5BD1F06476
Thanks!

They are type4 UUIDs. Basically just a random string that conforms to the type4 standard of uuids.

Related

Mongo: Sort objects in a collection with a timestamp in a different collection

I have some collection A with _id, content, timestamp as fields and some collection B with A_id, _id, content, timestamp as fields. A_id refers to some object in A.
I want to sort the objects in A based on their latest timestamps in B.
I can get it to work by re architecting my db design (e.g. storing a latest_B_timestamp in A) BUT is there a simple way to do this directly with Mongo?
Thanks!
I doubt there is any good way to do that with mongo. Your current solution seems ok and natural in mongo. Duplication is the way to go.
No.
MongoDB has no joins, so if 2 collections have related data, they should be worked in the application layer.

CouchDB and querying not by document ID

Is my understanding correct, that querying documents in CouchDB can only be done in a RESTful way by document ID but not by attributes (e.g. something like .../person?firstname=john).
For every query not by document ID I need a view (which means making a map function)?
Greetings Kudi
Yes, CouchDb does not support ad-hoc queries, but instead you define views by defining a map function. You are in full control of what data the map should produce: Id, Key, Value. You then can query this in a range of ways using key, keys, keyrange etc.
http://guide.couchdb.org/editions/1/en/views.html
http://docs.couchdb.org/en/latest/ddocs.html#view-functions

Change size of Objectid

In MongoDb ObjectId is a 12-byte BSON type.
Is there any way to reduce the size of objectID?
No. It's a BSON data type. It's like asking a 32-bit integer to shrink itself.
Every object must have _id property, but you are not restricted to ObjectId.
Every document in a MongoDB collection needs to have a unique _id but the value does not have to be an ObjectId. Therefore, if you are looking to reduce the size of documents in your collection you have two choices:
Pick one of the unique properties of your documents and use it as the _id field. For example, if you have an accounts collection where the account ID--provided externally--is part of your data model, you could store the account ID in the _id field.
Manage primary keys for the collection yourself. Many drivers support custom primary key factories. As #assylias suggests, going with an int will give you good space savings but, still, you will use more space than if you can use one of the fields in your model as the _id.
BTW, the value of an _id field can be composite: you can use an Object/hash/map/dictionary. See, for example, this SO question.
If you are using some type of object/model framework on top of Mongo, I'd be careful with (1). Some frameworks have a hard time with developers overriding id generation. For example, I've had bad experience with Mongoid in Ruby. In that case, (2) may be the safer way to go as the generation happens at the driver layer.

Is MongoDB _id unique by default?

Is MongoDB _id unique by default, or do I have to set it to unique?
For the most part, _id in mongodb is unique enough. There is one edge case where it's possible to generate a duplicate id though:
If you generate more than 16,777,215 object ids in the same single second, while running in one process while on the same machine then you will get a duplicate id due to how object ids are generated. If you change any one of those things like generating an id in a different process or on a different machine, or at a different unix time, then you won't get a duplicate.
Let me know if you ever manage to pull this off with a realistic use case. Apparently google only gets around 70,000 searches a second and those are all processed on different machines.
All documents contain an _id field. All collections (except for capped ones) automatically create unique index on _id.
Try this:
db.system.indexes.find()
ok .. short version
YES YES YES
_id uniqid by default , mongoDB creates index on _id by default and you do not need any settings
According to MongoDB's manual the answer is yes, it's unique by default:
MongoDB creates the _id index, which is an ascending unique index on the _id field, for all collections when the collection is created. You cannot remove the index on the _id field.
Share what I learned about this topic here:
Different from other RDBMS, Mongodb document's Id is generated on the client side. This functionality is usually implemented in the drivers of various programming languages.
The Id is string with 12 bytes length, which consists of several parts as follows:
TimeStamp(4 bytes) + MachineId(3 bytes) + ProcessId(2 bytes) + Counter(3 bytes)
Based on this pattern, it's extremely unlikely to have two Ids duplicated.

mongoDB - URL as document ID

Considering I want to create mongoDB documents for a bunch of distinct URLs: what would be the pros and cons (if any) of using the actual URL as the documents _id value instead of the default BSON ObjectId. Thanks in advance!
Cheers,
Greg
An overview of the subject here: http://www.mongodb.org/display/DOCS/Object+IDs
It has to be unique, you could potentially put yourself in the position of having to resolve collisions yourself. Better to leave the default _id alone and simply query against a field you're storing in the document, just how God (10gen) intended.
From http://www.mongodb.org/display/DOCS/BSON
The element name "_id" is reserved for use as a primary key id, but
you can store anything that is unique in that field. The database
expects that drivers will prevent users from creating documents that
violate these constraints.
From #mongodb
stupid _id values will probably make querying slow, but that's about it
And another user from #mongodb
Tell him the collisions will result in garbage data