使用push和slice更新数组

时间:2014-04-24 07:46:27

标签: mongodb mongodb-query

我刚刚开始使用MongoDB,并对如何更新数据库中的文档有一些疑问。我使用

在我的数据库中插入两个文档
db.userscores.insert({name: 'John Doe', email: 'john.doe@mail.com', levels : [{level: 1, hiscores: [90, 40, 25], achivements: ['capture the flag', 'it can only be one', 'apple collector', 'level complete']}, {level: 2, hiscores: [30, 25], achivements: ['level complete']}, {level: 3, hiscores: [], achivements: []}]});
db.userscores.insert({name: 'Jane Doe', email: 'jane.doe@mail.com', levels : [{level: 1, hiscores: [150, 90], achivements: ['Master of the universe', 'capture the flag', 'it can only be one', 'apple collector', 'level complete']}]});

我检查我的插入是否与find()命令一起使用,看起来没问题。

db.userscores.find().pretty();
{
        "_id" : ObjectId("5358b47ab826096525d0ec98"),
        "name" : "John Doe",
        "email" : "john.doe@mail.com",
        "levels" : [
                {
                        "level" : 1,
                        "hiscores" : [
                                90,
                                40,
                                25
                        ],
                        "achivements" : [
                                "capture the flag",
                                "it can only be one",
                                "apple collector",
                                "level complete"
                        ]
                },
                {
                        "level" : 2,
                        "hiscores" : [
                                30,
                                25
                        ],
                        "achivements" : [
                                "level complete"
                        ]
                },
                {
                        "level" : 3,
                        "hiscores" : [ ],
                        "achivements" : [ ]
                }
        ]
}
{
        "_id" : ObjectId("5358b47ab826096525d0ec99"),
        "name" : "Jane Doe",
        "email" : "jane.doe@mail.com",
        "levels" : [
                {
                        "level" : 1,
                        "hiscores" : [
                                150,
                                90
                        ],
                        "achivements" : [
                                "Master of the universe",
                                "capture the flag",
                                "it can only be one",
                                "apple collector",
                                "level complete"
                        ]
                }
        ]
}

如何向用户核心添加/更新数据?假设我想在级别1上向用户John Doe添加一个组件。如何插入hiscore 75并仍然将hiscore数组排序?我可以限制他的核心数量,这样阵列只包含3个元素吗?我试过

db.userscores.aggregate(
    // Initial document match (uses name, if a suitable one is available)
    { $match: {
        name : 'John Doe'
    }},

    // Expand the levels array into a stream of documents
    { $unwind: '$levels' },

    // Filter to 'level 1' scores 
    { $match: {
        'levels.level': 1
    }},

    // Add score 75 with cap/limit of 3 elements
    { $push: {
        'levels.hiscore':{$each [75], $slice:-3}
    }}
);

但它不会工作,我得到的错误是#34; SyntaxError:意外的令牌["。

另外,我如何获得1级所有用户的10个最高分?我的文档方案是否正常,或者我可以使用更好的方案在我的游戏中存储不同级别的用户hiscores和成就?使用上述方案进行查询或性能是否有任何缺点?

1 个答案:

答案 0 :(得分:1)

您可以使用以下声明添加分数:

db.userscores.update(
  { "name": "John Doe", "levels.level": 1 }, 
  { "$push": { "levels.$.hiscores": 75 } } )

这将对数组进行排序,因为只有当数组元素是文档时才支持。

在MongoDB 2.6中,您还可以对非文档数组使用排序:

db.userscores.update(
  { "name": "John Doe", "levels.level": 1 }, 
  { "$push": { "levels.$.hiscores": { $each: [ 75 ], $sort: -1, $slice: 3 } } } )
相关问题