【问题标题】:sails.js remove all members from collection waterlinesails.js 从集合水线中删除所有成员
【发布时间】:2019-01-22 06:35:56
【问题描述】:

我想删除集合拥有的所有成员,但我不想将每个成员 ID 传递给 .member() 方法。 waterline documentation 解释删除特定成员的方法,例如:

await User.removeFromCollection(3, 'pets')
.members([99,98]);

我想拥有这样的东西:

await User.removeFromCollection(3, 'pets')
.members(['*']);

【问题讨论】:

    标签: node.js collections sails.js waterline sails-mongo


    【解决方案1】:

    据我所知,这应该使用.destroy() 来完成,没有条件。

    编辑(2019-07-10):根据 noobular 的评论添加了空卷曲

    await User.destroy({});                // Removes all records from your User collection
    
    await User.destroy({name:'Bill'};    // Removes all records from your User collection
                                            where the 'name' is 'Bill'
    

    文档: .destroy()

    更新

    在您指出我误读了您的问题后,我想出了这个解决方案。 .removeFromCollection() 的文档指出,传入父 ID 数组将删除指定集合中的所有个子元素,但这似乎不像所写的那样起作用。

    不过,我确实使用 .replaceCollection() 为您找到了一个可行的解决方案。

    await User.replaceCollection(3, 'pets', []);
    
    OR
    
    await User.replaceCollection(3, 'pets').members([]);
    

    传入一个空数组将用空数组替换当前的关联数组,清除当前的关联。

    文档: .replaceCollection()

    【讨论】:

    • 我不想删除 user 它与我的 pets 有多对多关系,我只想删除关系
    • @Jafarrezaei 啊,对不起。我误读了你的问题。我会更新我的答案。
    • 谢谢,实际上今天我通过传递所需的成员 ID 来解决这个问题,但这是正确的答案并被接受。
    • 截至 2019 年 7 月 7 日,您将在使用 destroy() 时遇到错误。 Sails 会告诉您提供空标准,即 User.destroy({})。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多