MongoDB - How to search a Document that have two fields with the same key - mongodb

I have a document in production that have two fields with the same key, but i don't know the value of these fields, example:
{
"email":"email1#idk.com",
"email":"email2#idk.com"
}
When i'm trying to copy the collection to another database, it says that one document have a duplicate key. How can i search this document to mannually remove it? I've tried to run some aggregation querys and it doesn't work. Also, the collection have more than 1.000.000 of documents.

You can't have two fields with same key in a collection in MongoDB.
Your error on the duplicate key is not caused by this situation, but certainly by a unique index on the arrival collection.

Related

How to force insert document to mongodb

when I am inserting a document to MongoDB collection I get an error: E11000 duplicate key error collection
I would like to override the existing document with a new one.
Is there any way to force insert new data with the same _id field?
I am using go
You cannot insert two separate documents in MongoDB with the same _id field. This is stated in the documentation on document structure:
The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array.
What you're describing sounds more like an upsert, an operation that updates a document if it exists, and if it does not, inserts it instead. You could accomplish this by using updateOne and setting the upsert flag to true.

Making two fields compound index in mongoDb and update the document

I'm using mongoDb in our project and got stuck at a point.
I'm using bulkOperation of mongoDb to save a list of Objects.
I need to make two fields (mac and gatewaytime ) as a compound index on a collection that make up an unique combination for documents in collection.
I wanted to update the whole document if any combination of mac and gatewaytime found with a the new document.
This scenario is similer to the _id index created with each document. Just the difference is that here I need to make a compound index of these two fields.
I found that we can make an compund index as Unique but this just reject any document if found to be duplicate. I need this duplicate document to be updated with the new document in my case.
If question is not undestandable, please let me know freely.

create unique id in mongodb from last inserted id using pymongo

Is there a way I can find the last inserted document and the field, i.e. _id or id such that I can increment and use when inserting a new document?
The issue is that I create my own id count, but I do not store this, now I've deleted records, I cannot seem to add new records because I am attempting to use the same id.
There is no way to check insertion order in MongoDB, because the database does not keep any metadata in the collections regading the documents.
If your _id field is generated server-side then you need to have a very good algorithm for this value in order to provide collision avoidance and uniqueness while at the same time following any sequential constraints that you might have.

Does Mongodb automatically updates indexed items? [duplicate]

Lets say you have a collection with a field called "primary_key",
{"primary_key":"1234", "name":"jimmy", "lastname":"page"}
and I have an index on "primary_key".
This collection has millions of rows, I want to see how expensive is to change primary_key for one of the records. Does it trigger a reindex of the entire table? or does it just reindex the changed record? in either case is that expensive to do?
Updating an indexed field in mongodb causes an update of the index (or indices if you have more than one) that use it. It does not "reindex". Shouldn't be all that expensive - effectively you will delete the old entry and insert a new one.
This document has a fair amount of detail on mongodb indexes:
http://docs.mongodb.org/master/MongoDB-indexes-guide.pdf
BTW, keep in mind that there is one special field, _id, that mongodb uses as it's primary key
_id
A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document’s
primary key. If you create a new document without an _id field,
MongoDB automatically creates the field and assigns a unique BSON
ObjectId.
You cannot update the _id field.

Does mongodb reindex if you change the field that it is used in index?

Lets say you have a collection with a field called "primary_key",
{"primary_key":"1234", "name":"jimmy", "lastname":"page"}
and I have an index on "primary_key".
This collection has millions of rows, I want to see how expensive is to change primary_key for one of the records. Does it trigger a reindex of the entire table? or does it just reindex the changed record? in either case is that expensive to do?
Updating an indexed field in mongodb causes an update of the index (or indices if you have more than one) that use it. It does not "reindex". Shouldn't be all that expensive - effectively you will delete the old entry and insert a new one.
This document has a fair amount of detail on mongodb indexes:
http://docs.mongodb.org/master/MongoDB-indexes-guide.pdf
BTW, keep in mind that there is one special field, _id, that mongodb uses as it's primary key
_id
A field required in every MongoDB document. The _id field must have a unique value. You can think of the _id field as the document’s
primary key. If you create a new document without an _id field,
MongoDB automatically creates the field and assigns a unique BSON
ObjectId.
You cannot update the _id field.