MongoError:无法规范化查询:BadValue

时间:2016-02-02 23:25:16

标签: node.js mongodb

https://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#findOne

声明max选项与数字一起使用。

var result =
    yield myCol.findOne({
        lol: lol
    }, {
        max: 1
    });

我收到此错误:

 MongoError: Can't canonicalize query: BadValue $max must be a BSONObj
      at Function.MongoError.create (/home/karl/www/instantynode/src/node_modules/mongodb-core/lib/error.js:31:11)
      at queryCallback (/home/karl/www/instantynode/src/node_modules/mongodb-core/lib/cursor.js:266:34)
      at Callbacks.emit (/home/karl/www/instantynode/src/node_modules/mongodb-core/lib/topologies/server.js:95:3)
      at null.messageHandler (/home/karl/www/instantynode/src/node_modules/mongodb-core/lib/topologies/server.js:249:23)
      at Socket.<anonymous> (/home/karl/www/instantynode/src/node_modules/mongodb-core/lib/connection/connection.js:265:22)
      at emitOne (events.js:77:13)
      at Socket.emit (events.js:169:7)
      at readableAddChunk (_stream_readable.js:146:16)
      at Socket.Readable.push (_stream_readable.js:110:10)
      at TCP.onread (net.js:523:20)

编辑:刚刚意识到这种方法已经过折旧,使用find更好。

1 个答案:

答案 0 :(得分:1)

max是一种方法,不是一种选择,它需要一个BsonDocument作为输入:

var result =
    yield myCol.findOne({
        lol: lol
    }).max({field1:1});