Mongodb插入不带_id字段的文档 [英] Mongodb inserting doc without _id field

查看:625
本文介绍了Mongodb插入不带_id字段的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb的新手.

I am newbie to mongodb.

  1. 我需要插入一个不带_id字段自动生成的文档.

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

我需要将Tenant_id字段设置为唯一,或者需要将"_id"字段更改为Tenant_id.

I need to set the field Tenant_id as unique or need to change the "_id" field to Tenant_id.

该怎么做?

类似的东西

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

推荐答案

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

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

但是,通过显式将autoIndexId参数设置为false,可以在创建集合时更改此行为.

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 }

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

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" }

有关autoIndexId字段的更多信息,请参见 MongoDB文档.该页面是关于上限集合的,但是autoIndexId字段对于常规集合和上限集合都是通用的.

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.

这篇关于Mongodb插入不带_id字段的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆