donglin6109 2018-06-08 23:21
浏览 56


So I am trying to find a way where I can limit the amount of documents stored in a db of MongoDB. For example let's say that I have 10 documents in a db and have a new document inserted every 1 min but I only want 10 document in my db, what I am trying to figure out is what is the best way to solve this in, when enter a new document that it deletes the oldest document .

for example with id "1,2,3,4,5,6,7,8,9,10" and when id 11 gets inserted I want that id 1 gets deleted and when id 12 gets inserted i want that id 2 gets deleted and so on,

I tested the TTL function for MongoDB but it doesn't give me the result that I am looking for.

Any help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doupang9080 2018-06-08 23:39

    If you're using this lib you can do this by following code:

    if ($collection->count() > 10) {
        $collection->findOneAndDelete([], ['sort' => 'id']);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 基于ucc28019的pfc电路中芯片一直不工作
  • ¥15 yolov8在3588板子端c++推理报错
  • ¥50 unitywebrequest分段下载导致报错,如何解决?
  • ¥15 错误使用 gretna_GUI_PreprocessInterface>RunBtn_Callback
  • ¥15 WPF如何用Chart绘画出Y轴的左边数据
  • ¥15 pycharm无法查看内置代码
  • ¥15 跑hls xfopencv的例程standalone_hls_axi_example出的错误,csim没问题,c synthesis出的错误
  • ¥15 sqlserver update语句逐行生效
  • ¥20 Windows10系统命令行调用
  • ¥15 php环境如何实现国密SM2相关功能