How to remove _id in MongoDB and replace with another field as a Primary Key? - mongodb

I'm have a huge documents in a collection. I want to remove auto generated Object Id (_id key) from all the documents and replace it with another field as a Primary key?
I don't understand is why is there a need for a default Object Id in first place?

In mongodb each document must be unique, so you need an unique field to be used as id. If you do not provide one, mongodb will provide one for you automatically. However, if you want to give custom ids for whichever reason (improve query performance being one of them), you can do it manually. Here goes my suggestions:
If you are inserting a new document, you can manually set the _id field like:
doc._id = "12312312" //(Or whichever function to generate id you have)
doc.save(...)
But when you already have a document in the database, you cannot modify it anymore. What you can do is to make a copy of the document, save a new document with the same data and erase the old one:
// Fetch the documents
docs = db.clients.find({})
docs.forEach((doc) => {
//For each field you copy the values
new_doc.field = doc.field
new_doc._id = //YOUR ID FUNCTION
// insert the document, using the new _id
db.clients.insert(new_doc)
// remove the document with the old _id
db.clients.remove({_id: doc._id})
}
This question is similar to the following one:
How update the _id of one MongoDB Document?
Hope my answer was helpful

It is not possible to remove the _id field.
From https://docs.mongodb.com/manual/core/document/#the-id-field:
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.
What you can do is name your primary key as _id.

Related

How to force insert document to mongodb

when I am inserting a document to MongoDB collection I get an error: E11000 duplicate key error collection
I would like to override the existing document with a new one.
Is there any way to force insert new data with the same _id field?
I am using go
You cannot insert two separate documents in MongoDB with the same _id field. This is stated in the documentation on document structure:
The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
What you're describing sounds more like an upsert, an operation that updates a document if it exists, and if it does not, inserts it instead. You could accomplish this by using updateOne and setting the upsert flag to true.

i want to stop the creation of object id field in mongo db so that when i will insert the data into the collection

I want to prevent the creation of object id field in mongodb database so that the object id should not get created at the time of inserting the document into the collection
The _id field is required in every document:
In MongoDB, each document stored in a collection requires a unique _id
field that acts as a primary key. If an inserted document omits the
_id field, the MongoDB driver automatically generates an ObjectId for the _id field.
You can specify the _id value at the time of insertion to prevent the value from being auto-generated.

Does Mongodb automatically updates indexed items? [duplicate]

Lets say you have a collection with a field called "primary_key",
{"primary_key":"1234", "name":"jimmy", "lastname":"page"}
and I have an index on "primary_key".
This collection has millions of rows, I want to see how expensive is to change primary_key for one of the records. Does it trigger a reindex of the entire table? or does it just reindex the changed record? in either case is that expensive to do?
Updating an indexed field in mongodb causes an update of the index (or indices if you have more than one) that use it. It does not "reindex". Shouldn't be all that expensive - effectively you will delete the old entry and insert a new one.
This document has a fair amount of detail on mongodb indexes:
http://docs.mongodb.org/master/MongoDB-indexes-guide.pdf
BTW, keep in mind that there is one special field, _id, that mongodb uses as it's primary key
_id
A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document’s
primary key. If you create a new document without an _id field,
MongoDB automatically creates the field and assigns a unique BSON
ObjectId.
You cannot update the _id field.

Does mongodb reindex if you change the field that it is used in index?

Lets say you have a collection with a field called "primary_key",
{"primary_key":"1234", "name":"jimmy", "lastname":"page"}
and I have an index on "primary_key".
This collection has millions of rows, I want to see how expensive is to change primary_key for one of the records. Does it trigger a reindex of the entire table? or does it just reindex the changed record? in either case is that expensive to do?
Updating an indexed field in mongodb causes an update of the index (or indices if you have more than one) that use it. It does not "reindex". Shouldn't be all that expensive - effectively you will delete the old entry and insert a new one.
This document has a fair amount of detail on mongodb indexes:
http://docs.mongodb.org/master/MongoDB-indexes-guide.pdf
BTW, keep in mind that there is one special field, _id, that mongodb uses as it's primary key
_id
A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document’s
primary key. If you create a new document without an _id field,
MongoDB automatically creates the field and assigns a unique BSON
ObjectId.
You cannot update the _id field.

Mongodb - geospacial _id?

I currently have a collection of small documents. Each document has an indexed geospacial field and *the default _id is never used in any query*. There will never be more than one document related to a particular geo location. I think it makes sense to override the default _id, and use the geospacial data for this somehow.
Question is, how do you use geospacial data as the unique id? Is it a case of creating a flat string from the geo field? E.g. 'x123456y123456'?
The _id field is the unique identifier for each document and thus is a needed field. The _id field is generated on document creation automatically if one is not provided. If you can provide this geospaital value when creating the document you should be able to use the string as you suggested, you cannot use an array as the _id value. However please be aware that once a document is created the _id becomes unchangeable. This means that using the _id field as a meaningful index of geospatial data may not be of much value.
Have a look here for more info on the _id field and here for some information about creating geospatial indexes in Mongo