简体   繁体   English

如何添加到集合顶部/从集合底部删除(猫鼬)?

[英]How to add to top of collection/remove from bottom of collection (Mongoose)?

I'm using mongoose, and I trying to both add to the top of a collection and remove from the bottom of the collection.我正在使用 mongoose,我尝试添加到集合的顶部并从集合的底部删除。 I know that I can get around this by storing all of this in an array (then use push and pop), but to me, it seems like that just adds another unnecessary layer.我知道我可以通过将所有这些存储在一个数组中来解决这个问题(然后使用 push 和 pop),但对我来说,这似乎只是增加了另一个不必要的层。

By default, removing is from the beginning and adding adds the entry to the end.默认情况下,删除是从头开始,添加将条目添加到末尾。 Is there some way to add to the front of a collection and remove from the end?有没有办法添加到集合的前面并从最后删除?

Documents in a collection are not ordered, therefore there is no "top", "bottom", "front" or "back" to operate on.集合中的文档没有顺序,因此没有“顶部”、“底部”、“前”或“后”可操作。 Adding, removing or updating a document can change the order of other documents.添加、删除或更新文档可以更改其他文档的顺序。

To have documents in an order, add a field and sort by that field when querying.要按顺序排列文档,请在查询时添加一个字段并按该字段排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM