I am newbie to mongodb.
I need to insert a doc without the _id field generating automatically.
I need to set the field Tenant_id as unique or need to change the "_id" field to Tenant_id.
how to do it?
something like this
Tenant
{Tenant_id: 123, Tenant_info: ...}
By default, all regular collections automatically insert an _id field if it is absent.
Since MongoDB v4.0 there is no way to not have the _id
field
For MongoDB versions v4.0 and below:
However, this behavior can be changed when you create the collection, by setting explicitely the autoIndexId parameter to false.
> db.createCollection("noautoid", { autoIndexId: false })
{ "ok" : 1 }
Then you can insert documents without _id field. But some drivers, like the javascript one (and so the mongo console), add the _id field by themselves. In the mongo console, you can do this:
> db.noautoid._mongo.insert(db.noautoid._fullName, {name: "Jack"})
> db.noautoid.find()
{ "name" : "Jack" }
More information about the autoIndexId field can be found in the MongoDB documentation. This page is about Capped Collections but the autoIndexId field is common to both regular and capped collections.