我已经看到每当我们将任何值更新到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");
}
答案 0 :(得分: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"
}