Get the App
SLTechnology News&Howtos  ›  Database  › 

How to fix a set in MongoDB

Shulou Source: shulou.com Published: 2022-05-31 21:13:50 09月30日 Update

MongoDB how to fix the collection, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The so-called fixed set is to specify the size of the collection, and if the content to be saved exceeds the length of the collection, then LRU's algorithm (recent least use principle) will be used to remove the earliest data, thus saving the new data.

By default, a collection can be created using the createCollection () function, or automatically after adding data, but if you want to use a fixed collection, you must explicitly create an empty collection.

Example: create an empty collection (fixed collection)

> db.createCollection ("dept", {"capped": true, "size": 1024, "max": 5})

{"ok": 1}

Where "capped": true is represented as a fixed collection, "size": 1024 refers to the space capacity occupied by the collection (in bytes), and "max": 5 means that there can be up to five records.

Example: save 5 pieces of data to the collection

Db.dept.insert ({"deptno": 10, "dname": "Finance Department", "loc": "Beijing"})

Db.dept.insert ({"deptno": 11, "dname": "Finance Department", "loc": "Beijing"})

Db.dept.insert ({"deptno": 12, "dname": "Finance Department", "loc": "Beijing"})

Db.dept.insert ({"deptno": 13, "dname": "Finance Department", "loc": "Beijing"})

Db.dept.insert ({"deptno": 14, "dname": "Finance Department", "loc": "Beijing"})

> db.dept.find ()

{"_ id": ObjectId ("599504e70184ff511bf02be0"), "deptno": 10, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be1"), "deptno": 11, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be2"), "deptno": 12, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be3"), "deptno": 13, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e80184ff511bf02be4"), "deptno": 14, "dname": "Finance Department", "loc": "Beijing"}

Now that the upper limit of the collection has been reached, continue to save the new content:

> db.dept.insert ({"deptno": 16, "dname": "Finance Department", "loc": "Beijing"})

WriteResult ({"nInserted": 1})

> db.dept.find ()

{"_ id": ObjectId ("599504e70184ff511bf02be1"), "deptno": 11, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be2"), "deptno": 12, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e70184ff511bf02be3"), "deptno": 13, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599504e80184ff511bf02be4"), "deptno": 14, "dname": "Finance Department", "loc": "Beijing"}

{"_ id": ObjectId ("599505240184ff511bf02be5"), "deptno": 16, "dname": "Finance Department", "loc": "Beijing"}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Finance Beijing Finance Department data content examples help clarity upper limit function principle size byte capacity this situation article novice more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei Redmi OPPO Reno Microsoft