【问题标题】:Correct format of mongo return document on updateOne using mongo node driver使用 mongo 节点驱动程序在 updateOne 上正确格式的 mongo 返回文档
【发布时间】:2020-05-20 21:00:54
【问题描述】:

查看current mongo documentation,得到以下关于mongo updateOne return的解释:

Returns 该方法返回一个包含以下内容的文档:

matchedCount 包含匹配文档的数量 modifiedCount 包含修改文档的数量 upsertedId 包含 _id 用于更新插入的文档。如果操作运行时带有写关注,则布尔值被确认为真,如果写关注被确认为假 禁用

我正在使用 mongo 节点驱动程序从我的 javascript 代码中调用一个简单的 updateOne,结果得到以下文档:

let result = db.updateOne({ userName: "John" }, { $set: { age: 30 }}, { upsert: true});

CommandResult {
  result: { n: 1, nModified: 0, upserted: [ [Object] ], ok: 1 },
  connection: Connection {
    _events: [Object: null prototype] {
      commandStarted: [Function],
      commandFailed: [Function],
      commandSucceeded: [Function],
      clusterTimeReceived: [Function]
    },
    _eventsCount: 4,
    _maxListeners: undefined,
    id: 1,
    address: '127.0.0.1:27017',
    bson: BSON {},
    socketTimeout: 360000,
    monitorCommands: false,
    closed: false,
    destroyed: false,
    lastIsMasterMS: 25,
    [Symbol(description)]: StreamDescription {
      address: '127.0.0.1:27017',
      type: 'Standalone',
      minWireVersion: 0,
      maxWireVersion: 8,
      maxBsonObjectSize: 16777216,
      maxMessageSizeBytes: 48000000,
      maxWriteBatchSize: 100000,
      compressors: []
    },
    [Symbol(generation)]: 0,
    [Symbol(lastUseTime)]: 1590008021689,
    [Symbol(queue)]: Map {},
    [Symbol(messageStream)]: MessageStream {
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: true,
      bson: BSON {},
      maxBsonMessageSize: 67108864,
      [Symbol(buffer)]: [BufferList]
    },
    [Symbol(stream)]: Socket {
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'localhost',
      _readableState: [ReadableState],
      readable: true,
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: true,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: null,
      _server: null,
      timeout: 360000,
      _peername: [Object],
      [Symbol(asyncId)]: 32,
      [Symbol(kHandle)]: [TCP],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 360000,
        _idlePrev: [TimersList],
        _idleNext: [Timeout],
        _idleStart: 50804,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(asyncId)]: 42,
        [Symbol(triggerId)]: 32
      },
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0
    },
    [Symbol(ismaster)]: {
      ismaster: true,
      maxBsonObjectSize: 16777216,
      maxMessageSizeBytes: 48000000,
      maxWriteBatchSize: 100000,
      localTime: 2020-05-20T20:52:52.930Z,
      logicalSessionTimeoutMinutes: 30,
      connectionId: 488,
      minWireVersion: 0,
      maxWireVersion: 8,
      readOnly: false,
      ok: 1
    }
  },
  message: BinMsg {
    parsed: true,
    raw: <Buffer 6f 00 00 00 42 1f 03 00 26 00 00 00 dd 07 00 00 00 00 00 00 00 5a 00 00 00 10 6e 00 01 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 00 00 00 00 04 75 70 ... 61 more bytes>,
    data: <Buffer 00 00 00 00 00 5a 00 00 00 10 6e 00 01 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 00 00 00 00 04 75 70 73 65 72 74 65 64 00 29 00 00 00 03 30 00 21 00 ... 45 more bytes>,
    bson: BSON {},
    opts: { promoteLongs: true, promoteValues: true, promoteBuffers: false },
    length: 111,
    requestId: 204610,
    responseTo: 38,
    opCode: 2013,
    fromCompressed: undefined,
    responseFlags: 0,
    checksumPresent: false,
    moreToCome: false,
    exhaustAllowed: false,
    promoteLongs: true,
    promoteValues: true,
    promoteBuffers: false,
    documents: [ [Object] ],
    index: 95
  },
  modifiedCount: 0,
  upsertedId: { index: 0, _id: 5ec598d5c1eaed085d51cbe6 },
  upsertedCount: 1,
  matchedCount: 0
}

我错过了什么吗?如何从调用中获得成功返回(我想知道命令是成功还是失败)?

【问题讨论】:

  • 你期望它返回什么?更新的文件?因为结果中已经包含了你提到的字段。

标签: javascript mongodb


【解决方案1】:

您参考了 mongo shell 方法参考。

如果你使用的是 node.js 原生驱动,你可以找到 API 参考here

updateOne 的返回 result properties 与你得到的匹配。

【讨论】:

    【解决方案2】:

    尝试使用 findOneAndUpdate 将 returnNewDocument 选项设置为true

    let result = db.findOneAndUpdate({ userName: "John" }, { $set: { age: 30 }}, { upsert: true, returnNewDocument : true})
    

    如果你使用猫鼬: 将returnNewDocument 替换为new

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多