I don't understand Mongolia DB autoIncrement

Asked 2 years ago, Updated 2 years ago, 106 views

I heard that there is no AutoIncrement function in MongoDB, so when I looked it up, the reference document suggested a way to create another collection and implement it as a function.

https://docs.mongodb.com/manual/tutorial/create-an-auto-incrementing-field/#auto-increment-optimistic-loop

So I'm looking at the code, but I don't understand it well, so I'm posting a question.

function insertDocument(doc, targetCollection) {

    while (1) {

        var cursor = targetCollection.find( {}, { _id: 1 } ).sort( { _id: -1 } ).limit(1);

        var seq = cursor.hasNext() ? cursor.next()._id + 1 : 1;

        doc._id = seq;

        var results = targetCollection.insert(doc);

        if( results.hasWriteError() ) {
            if( results.writeError.code == 11000 /* dup key */ )
                continue;
            else
                print( "unexpected error inserting data: " + tojson( results ) );
        }

        break;
    }
}

I understand that cursors look for the last document, but then I don't know why I use hasNext. Isn't it AI if you just use +1 and use it as an _id? And I don't know why I need a counter collection.

One more question, is there a way to fill in the deleted id value when some of the documents are deleted? I'm thinking of a way to search from the beginning or index when it's deleted, but I wonder if there's any other way.

mongodb

2022-09-22 21:38

1 Answers

Answer to the first question

First of all, to explain the code above, Cursor is a kind of Iterator.

The problem can be divided into cases where the collection has no value at all and (B) more than one value.

For (B), the cursor absolutely contains the last value of the collection. Therefore, it can be imported from the cursor, which is the etherator, through the next() operation.

However, for (A), there is no value, so the next() operation causes an error.

HasNext() will be the way to know if there is a value that can be next() in the iterator, right?

Therefore, the above

var seq = cursor.hasNext() ? cursor.next()._id + 1 : 1;

The syntax is divided into the case where the last id value is obtained (hasNext is true) and the case where the first is registered in the collection (hasNext is false), and the first is to make the id 1.

For DBs that support normal transactions, use increasing the value of one column within the transaction. (To rule out the possibility of using the same ID).

When a new user is registered, it reads and uses the NextKey whose Seq is the user in the transaction, and stores the increased value again.

.

Answer to the second question

If you want to find the deleted id value, you have to look up the entire document, which will cost you a search. If you're sure that your overall data set (collection) stays on a constant scale, you might want to try it, but if it's likely to grow, the cost of searching will continue to increase, which will be quite inefficient as the cost (time) of adding new data will grow.

When it is deleted in the lane, you can think about recycling it by creating a deleted ID pool.

However, it is necessary to think about whether the ID is increasing and the shortage of available IDs is coming, such as whether the data is generated so much that the deleted ID needs to be recycled or whether the data is consumed quickly.


2022-09-22 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.