> For the complete documentation index, see [llms.txt](https://janeto.gitbook.io/mongodb-toan-tap/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://janeto.gitbook.io/mongodb-toan-tap/auto-increment-sequence-trong-mongodb/tao-ham-javascript.md).

# Tạo hàm JavaScript

Bây giờ, chúng ta sẽ tạo một hàm **getNextSequenceValue**, mà nhận tên dãy như là đầu vào của nó, tăng giá trị dãy thêm 1 và trả về số đã được cập nhật. Trong ví dụ này, tên dãy là **productid**.

```
>function getNextSequenceValue(sequenceName){

var sequenceDocument = db.counters.findAndModify(
{

query:{_id: sequenceName },

update: {$inc:{sequence_value:1}},

new:true

});
return sequenceDocument.sequence_value;

}
```
