2

我的数据结构是:

{
  "_id": { "$oid" : "511D0A0EC075F3FF25000003" },
  "progresses": [
    {
      "behavior": {
        "behavior": {
          "_id": "511d052f52fbf0fd25000002",
          "disabled": false,
          "name": "Unity Installed",
          "key": "UnityInstalled",
          "points": 1,
          "timeout": 0
        },
        "key": "UnityNotInstalled",
        "name": "Unity Not Installed",
        "points": 1,
        "timeout": 0,
        "disabled": false,
        "_id": { "$oid" : "511D056552FBF0FD25000003" }
      },
      "behaviorCount": 1,
      "behaviorParameter": null,
      "userId": null,
      "modifiedAt": { "$date": 1360857614000.000000 },
      "createdAt": { "$date": 1360857614000.000000 },
      "behaviorType": "user"
    },
    {
      "behavior": {
        "behavior": {
          "_id": "511cfac4955737a01f000001",
          "disabled": false,
          "name": "test",
          "key": "tt",
          "points": 1,
          "timeout": 0
        },
        "name": "Unity Installed",
        "key": "UnityInstalled",
        "points": 1,
        "timeout": 0,
        "disabled": false,
        "_id": { "$oid" : "511D052F52FBF0FD25000002" }
      },
      "behaviorCount": 3,
      "behaviorParameter": null,
      "userId": null,
      "modifiedAt": { "$date": 1360858565000.000000 },
      "createdAt": { "$date": 1360858553000.000000 },
      "behaviorType": "user"
    },
    {
      "behavior": {
        "behavior": {
          "_id": "511d052f52fbf0fd25000002",
          "disabled": false,
          "name": "Unity Installed",
          "key": "UnityInstalled",
          "points": 1,
          "timeout": 0
        },
        "name": "Active User",
        "key": "ActiveUser",
        "points": 1,
        "timeout": 0,
        "disabled": false,
        "_id": { "$oid" : "511D058E52FBF0FD25000004" }
      },
      "behaviorCount": 1,
      "behaviorParameter": null,
      "userId": null,
      "modifiedAt": { "$date": 1360858565000.000000 },
      "createdAt": { "$date": 1360858565000.000000 },
      "behaviorType": "user"
    },
    {
      "behavior": {
        "behavior": {
          "_id": "511d058e52fbf0fd25000004",
          "disabled": false,
          "name": "Active User",
          "key": "ActiveUser",
          "points": 1,
          "timeout": 0
        },
        "name": "Invite Count",
        "key": "InviteCount",
        "points": 1,
        "timeout": 0,
        "disabled": false,
        "_id": { "$oid" : "511D061752FBF0FD25000006" }
      },
      "behaviorCount": 1,
      "behaviorParameter": null,
      "userId": null,
      "modifiedAt": { "$date": 1360858587000.000000 },
      "createdAt": { "$date": 1360858587000.000000 },
      "behaviorType": "user"
    }
  ],
  "trophyAchievements": [

  ],
  "userId": "asfasd"
}

我想将所有progresses.behaviorCount 更新为1。我该怎么做?

4

2 回答 2

1

您应该使用 mongo 的位置运算符和点表示法。

在你的情况下,我会像这样使用 smth:

db.test.update({}, {$set: {"progresses.$.behaviorCount": "testing"}}, { multi: true })

但是,它不会更新嵌套数组中的所有字段,一次只更新一个。

查看类似问题:

作为一种解决方法,您可以遍历嵌套数组并按behaviorCount索引一次更新一个。

希望有帮助。

于 2013-03-21T15:26:29.250 回答
0

这个脚本就是答案:

var i = 0;
db.users.find().forEach(function(doc) {
    doc.progresses.forEach(function(progress) {
        progress.behaviorCount = 1;
    });

    db.users.save(doc);
    if (i % 10000 == 0) print(i);
    i++;
});
于 2013-03-22T02:00:11.083 回答