sails.js从集合水线中删除所有成员

时间:2019-01-22 06:35:56

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

我想删除集合具有的所有成员,但是我不想将每个成员ID传递给.member()方法。 waterline documentation介绍一种删除特定成员的方法,例如:

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

我想要某事:

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

1 个答案:

答案 0 :(得分:5)

据我所知,应该使用.destroy()而不使用任何条件。

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的数组将删除指定集合中的 all 个子代,但这似乎不像书面那样。

但是我确实使用.replaceCollection()为您找到了可行的解决方案。

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

OR

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

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

文档: .replaceCollection()