gpt4 book ai didi

javascript - 在一定数量后清空 Meteor 数据库/集合?

转载 作者:行者123 更新时间:2023-11-28 06:55:44 26 4
gpt4 key购买 nike

我可以说我是 JS 新手,但掌握了一切是如何工作的 - 但我最近尝试跳入 meteor ,它似乎只是删除了我难以理解的大部分快捷方式代码。

一旦 meteor 数据库达到一定数量的项目(在本例中为 100),我会尝试清空 meteor 数据库的前半部分。我只有:

var printOut = MongoDatabase.find().count(); 

if (printOut > 100) {
//delete first 50 entries of the 100 items
}

提前谢谢

最佳答案

首先找到 CreationDate 的截止日期,然后删除该日期之前的所有内容:

if (MongoDatabase.find().count() > 100){
cutoff = MongoDatabase.find({}, {fields: {CreationDate: 1, _id: 0}, sort: {CreationDate: -1 }, limit: 50}).fetch().pop().CreationDate
MongoDatabase.remove({CreationDate: {$lt: cutoff}})
}

这将在服务器上运行,但如果您尝试直接在客户端上执行此操作,您将收到以下错误:

"Not permitted. Untrusted code may only remove documents by ID. [403]"

因此,如果您需要从客户端触发此操作,使用 Meteor.Method 将是最好的方法。

关于javascript - 在一定数量后清空 Meteor 数据库/集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32584871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com