Exclude _id field in mongodb while inserting - mongodb

Is there any possibilities of without having _id field in mongodb collection??
I don't want it because i need to load mongodb data into apache pig, which will not support _id.
So, i just don't want _id field in my mongodb collections.
Anyone please help..
Thanks in advance.

No, you can't. The _id field is required for internal purposes in MongoDB. It is the MongoDB equivalent to a primary key in a relational database. Every document must have an unique _id field. It does not necessarily need to be an ObjectId, but it must be a value unique to the collection. But you can query data without the ID field:
db.yourCollection.find({ ...query... }, { _id: false } );

Related

Who generates Mongodb _id (ObjectId) field's value?

If I insert a document, directly with a mongodb client or with Mongoose, who is responsible to generate the _id field's value? Is it happening inside the database engine or the clients do the work?
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.
Note
Most MongoDB driver clients will include the _id field and generate an ObjectId before sending the insert operation to MongoDB; however, if the client sends a document without an _id field, the mongod will add the _id field and generate the ObjectId.
https://docs.mongodb.com/manual/core/document/index.html#the-id-field

Python: How do I provide my own ObjectID / _id when I insert into a MongoDB?

I'm inserting information regarding some files into a MongoDB, among other things a checksum for the file. I want to use the checksum as the _id for the document. How do I do that?
You would set the _id field explicitly on the document you are storing. For example:
document = {}
document['_id'] = my_id
Please note that the _id value has to be unique for every document. Only store checksum as _id if you know that it will be unique.

Hashed shard key in MongoDB on the _id so then what?

If I do create a hashed index with ensureIndex({ _id: "hashed"}) will Mongo know to take any queries on the _id field and run them against the hashed index? Or do I need to update all the queries that use _id to be the _id_hashed?
Mongodb doesn't modify the _id field when a hashed index is created. It will do the right thing, and query against _id appropriately. The hash will only be used to query and balance the shards.

How can i change _id field in MongoDB Collection to User_id?

I am new user for MongoDB Database. In MongoDb whatever insert into some collection defaultly one field is added that is _id field.
For Example:
db.users.insert({"User_id":"1","User_Name":"xxx","Address":"yyyy"})
db.users.find()
It shows
{ "_id" : ObjectId("528475fc326b403f580d2eba"), "User_id" : "1", "User_Name" : "xxx",Address" : "yyyy" }
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 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.
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?
and remember that _id means user_id and also keep in mind that this _id should be unique
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.
As for as I can understand your problem is that you want to use your mongoDB internal _id as your custom attribute. For example suppose the db contain the user Identity and having attributes like "_id , name , address ..." and you want to use this _id's value in your application as userId for external reference.
So as #SalvadorDali said _id field is really important in the mongoDB and you can not have a document without it. All you can do is let the db store the value by it's default _id but you can access outside using your own User_id by applying these two changes in your json file.
"properties": {
"userId":{
"type": "string",
"id":"true",
"index":"true",
"description": "unique id of identity"
}
}
now you store any unique value, it is stored in the db using default _id and outside you can have that value in userId field.
Correct me if i got your question wrong.

Mongodb - geospacial _id?

I currently have a collection of small documents. Each document has an indexed geospacial field and *the default _id is never used in any query*. There will never be more than one document related to a particular geo location. I think it makes sense to override the default _id, and use the geospacial data for this somehow.
Question is, how do you use geospacial data as the unique id? Is it a case of creating a flat string from the geo field? E.g. 'x123456y123456'?
The _id field is the unique identifier for each document and thus is a needed field. The _id field is generated on document creation automatically if one is not provided. If you can provide this geospaital value when creating the document you should be able to use the string as you suggested, you cannot use an array as the _id value. However please be aware that once a document is created the _id becomes unchangeable. This means that using the _id field as a meaningful index of geospatial data may not be of much value.
Have a look here for more info on the _id field and here for some information about creating geospatial indexes in Mongo