今天,我尝试使用$set收集$db DBRef。

但是服务器说:“ not okForStorage”,因此操作失败。

ps。我的mongod版本是2.2

{
  "ts" : ISODate("2013-01-16T14:05:42.814Z"),
  "op" : "update",
  "ns" : "mydb.coll1",
  "query" : {
    "_id" : NumberLong("928686353793211381")
  },
  "updateobj" : {

    "$set" : {
    "ref" : {
      "$db" : "db2",
      "$ref" : "coll2",
      "$id" : NumberLong("928686353793211381")
      }
    }
  },
  "nscanned" : 1,
  "keyUpdates" : 0,
  "numYield" : 0,
  "lockStats" : {
    "timeLockedMicros" : {
      "r" : NumberLong(0),
      "w" : NumberLong(200)
    },
    "timeAcquiringMicros" : {
      "r" : NumberLong(0),
      "w" : NumberLong(5)
    }
  },
  "exception" : "not okForStorage",
  "exceptionCode" : 12527,
  "millis" : 0,
  "client" : "10.168.89.9",
  "user" : ""
}

最佳答案

根据docs,DBRef中字段的顺序很重要。因此您需要将ref对象重新排序为:

"ref" : {
  "$ref" : "coll2",
  "$id" : NumberLong("928686353793211381"),
  "$db" : "db2"
}

10-07 18:47