繁体   English   中英

使用 mongo 节点驱动程序在 updateOne 上正确格式的 mongo 返回文档

[英]Correct format of mongo return document on updateOne using mongo node driver

查看当前的 mongo 文档,我得到以下关于 mongo updateOne 返回的解释:

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

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

我正在使用 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
}

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

您参考了 mongo shell 方法参考。

如果您使用的是 node.js 原生驱动程序,您可以在此处找到 API 参考

updateOne的返回结果属性与你得到的匹配。

在此处输入图像描述

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

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

如果您使用 mongoose:将returnNewDocument替换为new

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM