I have this collection
in mongo, but my question is is there any way that when doing one collection
with insert
no I get the _id
default since this time I use my id
as the second line.
{
"_id" : ObjectId("5ca1abb6ce037511f000628e"),
"id" : "sjohnson",
"name" : "Steve Johnson"
}
I need it to be like this:
{
"id" : "sjohnson",
"name" : "Steve Johnson"
}
According to the official documentation if your collection doesn't have a column
_id
explicitly; then MongoDB will create a column_id
and write a hexadecimal value of typeObjectId
.If you are going to handle the column with your own values,
_id
then you only have to make sure that said column receives unique values that are provided by yourself.EXAMPLE 1
Let's create an example database like this
Later, we execute the following command to insert a value into a users collection
If we execute a
find()
to said collection it will return thisResult
With the above you have been able to verify that if you explicitly declare a column
_id
, it will be respected and mongoDB will not add one by defaultEXAMPLE 2
Now we are going to insert a new record, but this time we will omit the column
_id
which will give way to the mongoDB manager to add the column by itself assigning a default valueIf I now run the following command
I will get the following as a result
The
_id
MongoDB field must be present in every document that is stored in the database. If a document does not have such a field at the time of performing ainsert
, MongoDB will create that field for us.Therefore we cannot get rid of said field. That is, you cannot remove it from your documents.
In your question, you state that you want to use your own field
id
. You have 2 options:You can override the field
_id
by sending the value for that field in your document. However, you must be able to guarantee that such a field is unique. Perhapssjohnson
it is not the most appropriate. If a user is calledSteve Johnson
and you generate the_id
taking the initial of the name and the last name, then,Sam Johnson
for example, it is already a name that you will not be able to store with this procedure. In many places the identity document number is used as a field_id
since a unique relationship is assumed. In other cases, email is used. Aninteger
autoincrement type value is also used. You already have something to pull from there. Just remember that when you insert your document, it must have a field_id
with a value that is unique to that document.You can ignore the fact that such a field exists, and skip it when making your query. You can also create your own field
id
, just as you currently do, and use the same value for bothid
,_id
always keeping in mind what I mentioned in the previous point.An example could be the following:
At the time of making the query, I can tell Mongo to skip bringing me the field,
_id
for example with the following queryfind
:As you can see, in the query I have passed as the second argument a document called parameter
projection
, which specifies the fields that I want the query to return. A value of0
(false
) indicates that the field will not be returned, and a value of1
(true
) indicates that it will be returned.When the parameter is omitted
projection
, MongoDB returns all fields in the document. On the other hand, if a parameter is passedprojection
indicating one (or some) value infalse
, the rest is assumedtrue
, except for the field_id
that is always assumedtrue
. That is what happens in the query that we just made.On the other hand, if we pass a parameter
projection
, indicating one (or some) values intrue
, the rest are assumed to befalse
(except the field_id
) and are not returned by the query.For example, if we want only the field
id
and the field,lastname
we can perform the following query:Finally, note that the only field that MongoDB uses as
PK
is the field_id
, this means that no other field can be set asPrimary Key
.In addition, MongoDB allows you to establish any other field such as
unique index
, (understand that notPrimary Key
) that must meet the necessary conditions to be considered a unique type index.I hope this clears your doubt.