如何将MongoDB集合中的_id字段更改为User_id? [英] How can i change _id field in MongoDB Collection to User_id?

查看:612
本文介绍了如何将MongoDB集合中的_id字段更改为User_id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB数据库的新用户.在MongoDb中,无论插入到哪个集合中,默认情况下都会添加一个字段,即_id字段.

I am new user for MongoDB Database. In MongoDb whatever insert into some collection defaultly one field is added that is _id field.

例如:

db.users.insert({"User_id":"1","User_Name":"xxx","Address":"yyyy"})
db.users.find()

它显示

{ "_id" : ObjectId("528475fc326b403f580d2eba"), "User_id" : "1", "User_Name" : "xxx",Address" : "yyyy" }

我不需要_id字段,我想用自动增量值将该_id字段替换为User_id. 是否有可能.请帮我.预先感谢.

I don't need _id field and i want to replace that _id field to User_id with auto increment values. Is it possible. Please help me. Thanks in advance.

推荐答案

_id字段实际上是

_id field is really special in mongodb. This is your primary key there and there is no way you can have a document without it. Even if you are trying to insert the document without it, mongo will create it for you (as in your example). Moreover, you can not even modify _id field for you collection.

但是您可以使用自己的_id创建文档.因此,如果您愿意,可以执行db.users.insert({"_id":"1","User_Name":"xxx","Address":"yyyy"}) \\why exactly 1 is a string?

But you can create a document with your own _id. So if you want you can do db.users.insert({"_id":"1","User_Name":"xxx","Address":"yyyy"}) \\why exactly 1 is a string?

,请记住,_id表示user_id,还请记住,该_id应该是唯一的

and remember that _id means user_id and also keep in mind that this _id should be unique

请记住,mongodb与sql不一样.它没有自动增量键(通过这种方式,我的意思是不是说创建者不知道如何做,而只是您可以不用它而已),但是您可以实现类似于相同的行为.

Keep in mind that mongodb is not like sql. It does not have autoincrement keys (by this I mean that it is not that creators did not know how to do it, but just that you can leave pretty much without it), but you can achieve create something that would resemble the same behaviour.

这篇关于如何将MongoDB集合中的_id字段更改为User_id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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