Mongodb inserting doc without _id field
Asked Answered
C

6

18

I am newbie to mongodb.

  1. I need to insert a doc without the _id field generating automatically.

  2. 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: ...}
Caitlin answered 11/9, 2012 at 21:40 Comment(1)
I'm also wondering about the _class field.Religiose
G
29

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.

Galvan answered 12/9, 2012 at 9:17 Comment(1)
It is no longer allowed - _id field is required for replication and therefore should always exist.Gownsman
S
4

The _id field is the default key for a mongo document unless you are using capped collections.

It is an umutable field which cannot be left out of a document structure.

I would recommend using tennant_id as _id instead.

Suborbital answered 11/9, 2012 at 21:59 Comment(0)
P
3

Mongodb will automatically generate the '_id' field only if you do not specify a value for the '_id' field when you insert a document. So you can just manually set the _id when you insert the document:

db.tenants.insert( {"_id" : 123, "Tenant_info" : ... } )
Pisciculture answered 11/9, 2012 at 21:59 Comment(0)
G
2

Every MongoDB document must have a unique field "_id" and if you don't provide it then it will be automatically generated for you.

It is not possible to rename this field.

Your two choices are:

1) ignore the generated _id field and use your own tenant_id field (you will need to add a unique index on that field) or 2) store tenant_id value in the _id field (and take advantage of the fact that it already has a unique index on it).

Gownsman answered 11/9, 2012 at 21:59 Comment(0)
R
1

There is no way to remove _id field in any mongodb collections. As, @stephene said,I tried the same, but iam getting error like this

> db.noautoid._mongo.insert(db.noautoid._fullName, {name: "Jack"});
 2015-06-18T11:22:29.149+0530 E QUERY    Error: insert needs 3 args
 at (shell):1:20
Rayford answered 18/6, 2015 at 6:47 Comment(0)
H
1

Mongo states in https://www.mongodb.com/docs/manual/reference/method/db.createCollection/#definition

Starting in MongoDB 4.0, you cannot set the option autoIndexId to false when creating collections in databases other than the local database.

enter image description here

Hallowmas answered 17/10, 2022 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.