使用 MongoDB 数组作为堆栈 [英] Use MongoDB array as stack

查看:55
本文介绍了使用 MongoDB 数组作为堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人用MongoDB的Array类型来实现栈?

Has anybody used MongoDB's Array type to implement a stack?

我知道我可以像这样append到一个数组:

I know that I can append to an Array like so:

db.blogposts.update( {_id:5}, {$push: {comments: {by: "Abe", text:"First"}}})

这里,数组的末尾是堆栈的顶部......我没有看到用第零个索引处的堆栈顶部来实现这一点的方法,但我很想错.

Here, the end of the array is the top of the stack... I don't see a way to implement this with the top of the stack at the zero'th index, but I'd love to be wrong.

而且我知道我可以像这样查看数组的最后一个值:

And I know that I can peek at the last value of the the array like so:

db.blogposts.find( {_id:5}, {comments: {$slice:-1}})

有了这样的实现,我可以在 MongoDB 更新语句中窥视"堆栈顶部吗?这会给我的语义,如果堆栈的顶部是 X,则将该项目推入堆栈".我需要这是一个原子操作!

With an implementation like this, can I "peek" at the top of the stack in a MongoDB update statement? That would give me the semantic, "push this item on the stack if the top of the stack is X". I need this to be an atomic operation!

任何建议表示赞赏.谢谢!

Any advice appreciated. Thanks!

推荐答案

不幸的是,目前没有办法完全按照您的描述执行此操作.

Unfortunately, there is currently no way to do this exactly as you have described.

正如 Chris Shain 指出的那样,https://jira.mongodb.org/browse/SERVER-2191 - $push() 到数组前面"和类似的 https://jira.mongodb.org/browse/SERVER-1824 - 支持插入特定数组索引"会有所帮助,但这些功能目前还没有针对特定的发行版本.

As Chris Shain pointed out, https://jira.mongodb.org/browse/SERVER-2191 - "$push() to front of array" and similarly https://jira.mongodb.org/browse/SERVER-1824 - "Support for inserting into specific array index" would help, but these features are currently not slated for a specific release version.

作为一种可能的解决方法,您可以在文档中添加一个名为lastElement"(或等效项)的字段,其中包含推送到数组的最后一个元素的副本.在您的更新语句中,您可以查询lastElement"值,如果匹配,则同时将其设置为新值,并在单个原子操作中将相同值推送到数组.

As a possible work-around, you could add a field named "lastElement" (or equivalent) to your document, which contains a copy of the last element pushed to the array. In your update statement, you could then query against the "lastElement" value, and if it matches, simultaneously set it to the new value and push the same value to the array in a single, atomic operation.

例如:

> db.blogposts.save({_id:5, comments:[{by: "Abe", text:"First"}], lastElement:{by: "Abe", text:"First"}})
> db.blogposts.find().pretty()
{
    "_id" : 5,
    "comments" : [
        {
            "by" : "Abe",
            "text" : "First"
        }
    ],
    "lastElement" : {
        "by" : "Abe",
        "text" : "First"
    }
}
> db.blogposts.update({"lastElement.text":"First"}, {$set:{lastElement:{by: "Joe", text:"Second"}}, $push:{comments:{by: "Joe", text:"Second"}}})
> db.blogposts.find().pretty()
{
    "_id" : 5,
    "comments" : [
        {
            "by" : "Abe",
            "text" : "First"
        },
        {
            "by" : "Joe",
            "text" : "Second"
        }
    ],
    "lastElement" : {
        "by" : "Joe",
        "text" : "Second"
    }
}
> 

作为替代方案,您可以考虑原子操作"文档的如果当前更新"部分中概述的策略:http://www.mongodb.org/display/DOCS/Atomic+Operations

As an alternative, you may consider the strategy outlined in the "Update if Current" section of the "Atomic Operations" documentation: http://www.mongodb.org/display/DOCS/Atomic+Operations

我意识到这些是变通办法,而不是理想的解决方案.希望以上内容可以帮助您实现目标,或者至少为您提供一些思考以提出不同的解决方案.如果您这样做了,请在此处分享,以便任何可能遇到类似问题的社区成员都能从您的经验中受益.谢谢.

I realize these are work-arounds and not ideal solutions. Hopefully the above will help you to accomplish your goal, or at least provide some food for thought for you to come up with a different solution. If you do, please share it here so that any members of the Community who may be experiencing similar issues may have the benefit of your experience. Thanks.

这篇关于使用 MongoDB 数组作为堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆