mongodb Mongodb插入没有_id字段的文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12378320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:48:57  来源:igfitidea点击:

Mongodb inserting doc without _id field

mongodbmongodb-javamongodb-query

提问by Ramya

I am newbie to mongodb.

我是 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.

  1. 我需要插入一个没有 _id 字段自动生成的文档。

  2. 我需要将字段 Tenant_id 设置为唯一或需要将“_id”字段更改为 Tenant_id。

how to do it?

怎么做?

something like this

像这样的东西

     Tenant
        {Tenant_id: 123, Tenant_info: ...}

回答by Stephane Godbillon

By default, all regular collections automatically insert an _id field if it is absent.

默认情况下,如果不存在 _id 字段,所有常规集合都会自动插入。

However, this behavior can be changed when you create the collection, by setting explicitely the autoIndexId parameter to false.

但是,可以在创建集合时更改此行为,方法是将 autoIndexId 参数显式设置为 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:

然后您可以插入没有 _id 字段的文档。但是一些驱动程序,例如 javascript 驱动程序(以及 mongo 控制台),会自行添加 _id 字段。在 mongo 控制台中,您可以执行以下操作:

> db.noautoid._mongo.insert(db.noautoid._fullName, {name: "Hyman"})
> db.noautoid.find()
{ "name" : "Hyman" }

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.

有关 autoIndexId 字段的更多信息可以在MongoDB 文档中找到。此页面是关于封顶集合,但 autoIndexId 字段对于常规集合和封顶集合都是通用的。

回答by Sammaye

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

_id除非您使用上限集合,否则该字段是 mongo 文档的默认键。

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

它是一个不能被排除在文档结构之外的可变字段。

I would recommend using tennant_idas _idinstead.

我建议使用tennant_idas_id代替。

回答by Louisa

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:

仅当您在插入文档时未为 '_id' 字段指定值时,Mongodb 才会自动生成 '_id' 字段。因此,您可以在插入文档时手动设置 _id:

db.tenants.insert( {"_id" : 123, "Tenant_info" : ... } )

回答by Asya Kamsky

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

每个 MongoDB 文档都必须有一个唯一的字段“_id”,如果您不提供它,它将自动为您生成。

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).

1) 忽略生成的 _id 字段并使用您自己的 tenant_id 字段(您需要在该字段上添加唯一索引)或 2)在 _id 字段中存储 tenant_id 值(并利用它已经具有唯一索引的事实在上面)。

回答by Jagadeesh

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

无法删除任何 mongodb 集合中的 _id 字段。正如@stephene 所说,我尝试了同样的方法,但我收到了这样的错误

> db.noautoid._mongo.insert(db.noautoid._fullName, {name: "Hyman"});
 2015-06-18T11:22:29.149+0530 E QUERY    Error: insert needs 3 args
 at (shell):1:20