2

我已经看到,每当我们将任何值更新到 mongo 集合中时。更新后,它会按字母顺序重新排列键。假设当前模式是

  _id:"Objectid(blah blah)"
     id: "1", 
fullname: "user",
 username: "username", 
password: "password",

更新后变成

 _id:"Objectid(blah blah)"
    fullname:"user"
    id:"1"
    password:"password"
    username:"user"

我知道这没有问题。这不会影响任何事情,因为数组数据仍然与值相对应。但我只是想知道为什么会这样。

编辑:

这是mongoshell中的更新函数代码。但这并没有帮助我

function (query, obj, upsert, multi) {
    assert(query, "need a query");
    assert(obj, "need an object");
    var firstKey = null;
    for (var k in obj) {
        firstKey = k;
        break;
    }
    if (firstKey != null && firstKey[0] == "$") {
        this._validateObject(obj);
    } else {
        this._validateForStorage(obj);
    }
    if (typeof upsert === "object") {
        assert(multi === undefined, "Fourth argument must be empty when specifying upsert and multi with an object.");
        opts = upsert;
        multi = opts.multi;
        upsert = opts.upsert;
    }
    this._db._initExtraInfo();
    this._mongo.update(this._fullName, query, obj, upsert ? true : false, multi ? true : false);
    this._db._getExtraInfo("Updated");
}
4

1 回答 1

2

文档说它们在文档大小增加超过分配的大小时重新排序,但是,似乎只要文档大小发生变化,它们就会重新排序(如果大小相同,它不会改变)。

例如,这里第一个命令不变,但第二个命令不变。

> db.t3.insert({_id : 1234',id:'xxx',
fullname:'use1',password:'password','username':'user'})
> db.t3.update({'_id':'1234'},{$set : {fullname:'use1'}})
> db.t3.findOne()
{
    "_id" : "1234",
    "id" : "xxx",
    "fullname" : "use1",
    "password" : "password",
    "username" : "user"
}
> db.t3.update({'_id':'1234'},{$set : {fullname:'use11'}})
> db.t3.findOne()
{
    "_id" : "1234",
    "fullname" : "use11",
    "id" : "xxx",
    "password" : "password",
    "username" : "user"
}

看这里https://jira.mongodb.org/browse/SERVER-2592

于 2013-03-16T23:08:51.843 回答