Mongodb 使用推送和切片更新数组

Mongodb 使用推送和切片更新数组,mongodb,mongodb-query,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'

我刚刚开始使用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"
                        ]
                }
        ]
}
如何向我的userscores添加/更新数据?假设我想在第1级向用户John Doe添加一个hiscore。如何插入hiscore 75并仍对hiscore数组进行排序?我可以限制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}
    }}
);
但它不起作用,我得到的错误是“SyntaxError:Unexpected token[”


此外,我如何从级别1的所有用户那里获得10分最高的分数?我的文档方案可以吗?或者我可以使用更好的方案来存储用户在不同级别上的核心和成就以用于我的游戏?使用他们的方案进行查询或表现是否有任何缺点?

您可以用以下语句添加分数:

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 } } } )

嗨,塞巴斯蒂安,它几乎可以工作:)在更新命令之后,我得到了他的内核[75 40 25],但它应该是[90 75 40],原始的内核数组是[90 40 25]。任何ide都可以这样做吗?对不起,$slice应该是
3
而不是
-3