MongoDB - 添加到集合并递增
我正在尝试使用 MongoDB 计算单词使用情况。我的集合目前看起来像这样:
{'_id':###, 'username':'Foo', words:[{'word':'foo', 'count':1}, {'word':'bar', 'count':1}]}
当发布新帖子时,我将所有新单词提取到一个数组中,但我试图找出更新插入到单词数组并增加计数(如果单词已经存在)。
例如,在上面的示例中,如果用户“Foo”发布“lorem ipsum foo”,我会将“lorem”和“ipsum”添加到用户单词数组中,但增加“foo”的计数。
这可以在一个查询中实现吗?目前我正在使用 addToSet:
'$addToSet':{'words':{'$each':word_array}}
但这似乎没有提供任何增加字数的方法。
非常感谢一些帮助:)
I am trying to count word usage using MongoDB. My collection currently looks like this:
{'_id':###, 'username':'Foo', words:[{'word':'foo', 'count':1}, {'word':'bar', 'count':1}]}
When a new post is made, I extract all the new words to an array but I'm trying to figure out to upsert to the words array and increment the count if the word already exists.
In the example above, for example, if the user "Foo" posted "lorem ipsum foo", I'd add "lorem" and "ipsum" to the users words array but increment the count for "foo".
Is this possible in one query? Currently I am using addToSet:
'$addToSet':{'words':{'$each':word_array}}
But that doesn't seem to offer any way of increasing the words count.
Would very much appreciate some help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您愿意从列表切换到哈希(对象),您可以自动执行此操作。
来自 docs:“
$inc
... 增量字段如果对象中存在字段,则按数值,否则将字段设置为数值。”因此,如果您可以将容器和对象重构为:
,
则可以使用操作
update
with: ,如果 'foo' 不存在,它将创建
{ 'foo': 1 }
不存在,否则增加 foo。例如:
If you're willing to switch from a list to hash (object), you can atomically do this.
From the docs: "
$inc
... increments field by the number value if field is present in the object, otherwise sets field to the number value."So, if you could refactor your container and object:
to:
you could use the operation
update
with:which would create
{ 'foo': 1 }
if 'foo' doesn't exist, else increment foo.E.g.:
不幸的是,不可能通过您的架构的一次更新来完成此操作。您的模式有点问题,可能应该转换为具有字计数器的专用集合,例如:
这将避免很多问题,例如:
两种情况都需要两次更新才能完成您想要的操作,这会引入原子性问题。通过循环访问 word_array 来更新每个单词更好、更安全(并且这两种解决方案都是可能的)。
Unfortunately it is not possible to do this in a single update with your schema. Your schema is a bit questionable and should probably be converted to having a dedicated collection with word counters, e.g :
That will avoid quite a few issues such as :
Both scenarios require two updates to do what you want which introduces atomicity issues. Updating per word by looping through word_array is better and safer (and is possible with both solutions).