简体   繁体   English

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

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

Looking at the current mongo documentation , I get the following explanation on mongo updateOne return:查看当前的 mongo 文档,我得到以下关于 mongo updateOne 返回的解释:

Returns The method returns a document that contains: Returns 该方法返回一个包含以下内容的文档:

matchedCount containing the number of matched documents modifiedCount containing the number of modified documents upsertedId containing the _id for the upserted document. matchCount 包含匹配文档的数量 modifiedCount 包含修改文档的数量 upsertedId 包含更新插入文档的 _id。 A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled boolean 如果操作运行时带有写关注则被确认为真,如果写关注被禁用则被确认为假

I'm calling a simple updateOne from my javascript code using the mongo node driver and getting the following document as a result:我正在使用 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
}

Am I missing something?我错过了什么吗? How can I get a success return from the call (I wish to know if the command succeedeed or failed)?如何从调用中获得成功返回(我想知道命令是成功还是失败)?

You referred to the mongo shell method reference.您参考了 mongo shell 方法参考。

If you are using the node.js native driver, you can find the API Reference here如果您使用的是 node.js 原生驱动程序,您可以在此处找到 API 参考

The return result properties of updateOne matches with what you got. updateOne的返回结果属性与你得到的匹配。

在此处输入图像描述

Try using findOneAndUpdate setting returnNewDocument option to true尝试使用 findOneAndUpdate 将returnNewDocument选项设置为true

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

If you use mongoose: replace returnNewDocument with new如果您使用 mongoose:将returnNewDocument替换为new

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

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