本文介绍了使用MongoDB C#驱动程序进行存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MongoDb中实现一种称为桶化的技术(或者在MongoDB研讨会中被称为桶化技术),并且它使用Push和Slice来实现这一点.这是为了实现类似于twitter/facebook的用户供稿系统.

I'm trying to implement a technique known as bucketing in MongoDb (or so it was referred to as in a MongoDB workshop) and it uses the Push and Slice to achieve this. This is to achieve a user feed system similar to that of twitter/facebook.

基本上,我有一个包含一系列项目(提要项目)的文档.我要在用户的数量达到一定数量时创建一个新文档.

Essentially I have a document with an array of items (feed items). I want to create a new document when this number of items reaches a certain number for a user.

因此,如果最新的userFeed文档集合包含50个项目,则我希望创建一个新文档,并将新项目插入到新创建的文档的项目数组中.

So, if the latest userFeed document's collection has 50 items, i want a new document to be created and the new item to be inserted into the item array of the newly created document.

这是我到目前为止的代码:

This is the code i have thus far:

var update = Builders<UserFeed>
                    .Update
                    .CurrentDate(x => x.DateLastUpdated)
                    .PushEach(x =>
                        x.Items,
                        new List<FeedItemBase> { feedItem },
                        50);

                var result = await Collection.UpdateOneAsync(x =>
                    x.User.Id == userFeedToWriteTo,
                    update,
                    new UpdateOptions { IsUpsert = true }
                    ).ConfigureAwait(false);

...

但是它似乎没有创建新文档,甚至没有将其插入现有文档的数组中.我认为新文档的创建将由此

But it does not appear to create a new document, or even insert the item into the existing document's array. I thought the creation of the new document would be handled by this

new UpdateOptions { IsUpsert = true }

但显然不是.任何帮助将不胜感激

but apparently not. Any help would be greatly appreciated

推荐答案

因此,在写出问题并大声说了几次之后,我意识到了问题所在.

So after having written out the problem and saying it out loud a few times i realised what the problem was.

我需要在主用户供稿文档上添加一个计数器,每次添加项目(发布供稿项目)时都需要增加一个计数器.在执行更新/更新的查询中,我只需要检查< 50.之后,一切都会按预期进行.这是更正的代码

I needed a counter on the main userfeed document which needed to be incremented every time an item was added (a feed item was posted). And in my query to perform the update/upsert i just needed to check for < 50. After which, everything works as expected. Here is the corrected code

var update = Builders<UserFeed>
                    .Update
                    .CurrentDate(x => x.DateLastUpdated)
                    .PushEach(x =>
                        x.Items,
                        new List<FeedItemBase> { feedItem },
                        50)
                        .Inc(x => x.Count, 1);

                var result = await Collection.UpdateOneAsync(x =>
                    x.User.Id == userFeedToWriteTo && x.Count < 50,
                    update,
                    new UpdateOptions { IsUpsert = true }
                    ).ConfigureAwait(false);

并且,如果从项数组中删除了用户供稿项,则只要不更正计数,一切都将按预期工作.但是,如果您修改删除次数,则会出现问题,因为最终将有项目添加到以前的文档中,并且您需要在展开数据之后执行排序,目前我不需要这样做.这确实意味着您最终将得到一些数组中少于50个项目的文档,但是对我而言这并不重要.

And as long as the count is NOT corrected if a user feed item is deleted from the items array, everything should work as expected. However, problems will arise if you modify the count on removal because you will end up with items added to previous documents and you will need to perform sorts after you unwind the data, which at present, i do not need to. It does mean you will end up with some documents with less than 50 items in the array, but to me that doesn't really matter.

我希望这对尝试在c#中实现类似解决方案的人有所帮助.

I hope this helps someone trying to implement a similar solution in c#.

这篇关于使用MongoDB C#驱动程序进行存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:04