如何获取 MongoDB 中子数组的最小值/最大值?

发布于 2024-11-09 02:42:49 字数 956 浏览 0 评论 0原文

因此,我遇到了使用数组标记文档的情况,例如:

tags: [
   'Housing' : 10,
   'Retail'  : 1,
   'Stocks'  : 25,
]

我只是保存标签本身,但最近添加了数字,因为我需要知道排名/位置。因此,标签旁边的数字代表该文档在标有该标签的文档集中的排名。

在一些地方它会变得棘手,但现在我只是想弄清楚如何添加带有一个或多个这些标签的另一个文档。假设我创建了一个新文档并将其标记为 Housing。它的排名需要设置为 11,但我怎么知道呢?

到目前为止,我能找到的唯一解决方案是执行映射/归约来遍历记录并找到该标签的最大值,添加一个并保存。现在,大多数记录可能每条只有 2-3 个标签,但理论上可能有 10-15 个。不管怎样,一旦有很多标签和很多记录,map/reduce 似乎就需要付出巨大的努力......

有没有更简单的方法,或者我应该开始寻找这个问题的另一种解决方案?

编辑:

让我向您提供有关我正在尝试解决的问题的更多详细信息...我正在幻灯片/轮播中显示图像。幻灯片涵盖了不同的类别,因此使用上面的类别/标签,您可以查看所有图像,也可以仅查看来自住房、零售、股票等的图像。现在我只有少数定义的类别,但它相当这些可能会随着时间的推移而扩大。它们需要按标签过滤,并按日期排序(最新的在前)。

现在,到目前为止我一直在这样做。当我想在甲板中间选择任意图像时,问题就出现了。假设您要加载 6 个月前上传的“housing_chart.gif”。我不想加载 6 个月的图像来获取该图像(这基本上就是我现在正在做的事情)。相反,我想加载该特定图像,然后能够对下一个/上一个图像进行分页。

但为了对轮播上的图像进行“分页”,我必须知道该图像在结果中的位置......而无需实际获取所有结果并进行计数。所以我认为对它们进行排名是可行的方法,但这也会导致其他问题。我真的不喜欢仅仅为了存储排名而创建另一个集合的想法,但这可能是最有效的方法。

So I have a situation where I'm tagging documents using an array, such as:

tags: [
   'Housing' : 10,
   'Retail'  : 1,
   'Stocks'  : 25,
]

I was just saving the tags themselves, but have recently added the numbers because I need to know rank/position. So the number next to the tag stands for the rank of the document in the set of documents marked with that tag.

It gets tricky in a few places, but right now I'm just trying to figure out how I add another document with one or more of these tags. Let's say I create a new doc and tag it as Housing. Its rank needs to be set to 11, but how do I know that?

The only solution I've been able to find so far is to do a map/reduce to go through the records and find the max value for that tag, add one and save. Now, most records will probably only have 2-3 tags each, but it's theoretically possible to have 10-15. Either way it seems like map/reduce would take a herculean effort once there are a lot of tags and a lot of records...

Is there an easier way or should I just start looking for another solution to this problem?

Edit:

Let me give you a little more detail about the problem I'm trying to solve... I'm displaying images in a slideshow/carousel. The slideshow covers different categories, so using the category/tags from above, you can either view ALL of the images, or just those from Housing, Retail, Stocks, etc. Right now I only have a handful of defined categories, but it's quite possible that these will expand over time. They need to be filtered by the tag, and sorted by date (newest first).

Now, up until this point I've been doing just that. The problem comes in when I want to select an arbitrary image in the middle of the deck. Say you want to load "housing_chart.gif" that was uploaded 6 months ago. I don't want to load 6 months of images in order to get to that image (which is basically what I'm doing now). Instead I want to load that particular image and then be able to paginate it for next/previous images.

But in order to "paginate" the images on the carousel, I have to know the location of that image in the results... without actually getting all of the results and counting. So I figured putting a rank on them would be the way to go, but that causes other problems as well. I don't really like the idea of creating another collection just to store ranks, but that may be the most efficient way of going about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2024-11-16 02:42:49

你需要军衔做什么?这个有更新过吗?如果不是,并且排名始终在增加,您不能只按日期对特定标签的文档进行排序吗?

我认为有关排名的信息确实属于“标签”而不是文档,即。每个标签应该有一个与该标签关联的文档列表 - 然后可以通过该列表中的位置来定义该列表。

What do you need the rank for? Is this ever updated? If not and if the rank is always increasing, can't you just sort the documents for a particular tag by their date?

I would argue that information about the rank really belongs to the "tag" rather than the document, ie. each tag should have an associated list of documents with this tag - the position in that list can then define the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文