Golang 和 Mgo 按 $natural 排序:-1

Golang and Mgo sort by $natural: -1

我只是想在 MongoDB 中使用 Golang 和 Mgo 在我的 collection 中获取最新文档。

我的 collection 中的文档:

{
    "_id",
    "numbers" : {
        "fst",
        "snd",
        "thd"
    },
    "none" : [ 
        {
            "fst",
            "snd",
            "thd",
            "email"
        }
    ],
    "twoNums" : [ 
        {
            "fst",
            "snd",
            "thd",
            "email"
        }
    ],
    "threeNums" : [ 
        {
            "fst",
            "snd",
            "thd",
            "email"
        }
    ]
}

我试过了:

err := db.C("plays").Find(nil).Select(bson.M{"numbers": 1}).Sort("$natural:-1").Limit(1).One(&numbs)

并且 space 介于 $natural 和“-1”之间

err := db.C("plays").Find(nil).Select(bson.M{"numbers": 1}).Sort("$natural: -1").Limit(1).One(&numbs)

在 MongoDB shell 中完美运行

db.getCollection('plays').find({}, {numbers: 1}).sort({$natural: -1}).limit(1)

使用

db.C("plays").Find(nil).Select(bson.M{"numbers": 1}).Sort("$-natural").Limit(1).One(&numbs)

查看 code 我想它应该是 -$natural 用于反向排序:

err := db.C("plays")
  .Find(nil)
  .Select(bson.M{"numbers": 1})
  .Sort("-$natural")
  .Limit(1)
  .One(&numbs)