2017-06-07 48 views
1

在mongoskin中插入mongoskin時,我可以通過以下方法獲取對象ID。這是工作absoultely以及更新後的對象ID命令mongoskin

 db.collection('test').insert({ 
    version: nc.version, 
    }, function (err, result) { 
// This would give me objectID of nre 
    console.log('creating new version ', result.ops[0]._id); 
     } 
    }) 

雖然與{UPSERT:真正}更新在收集我找不到任何

  db.collection('test').update({labId: lab_id},{$set: 
      { 
      version: nc.version, 
     }},{upsert:true}, function (error, result) { 

      if (error){ 
       // If there is error then 
       console.log("Error occured while adding in system"); 
      } 
      console.log('Result is ', result); 
     }) 

結果是什麼打印是這樣的。所以,我想知道在哪裏可以找到我的OBJECTID(即_id我們得到對象MongoDB中添加後)

Result is { 
    result: { n: 1, nModified: 0, upserted: [ [Object] ], ok: 1 }, 
    connection: 
    Connection { 
    domain: null, 
    _events: 
     { error: [Object], 
     close: [Object], 
     timeout: [Object], 
     parseError: [Object] }, 
    _eventsCount: 4, 
    _maxListeners: undefined, 
    options: 
     { host: 'localhost', 
     port: 27017, 
     size: 5, 
     connectionTimeout: 30000, 
     socketTimeout: 30000, 
     keepAlive: true, 
     keepAliveInitialDelay: 0, 
     noDelay: true, 
     ssl: false, 
     checkServerIdentity: true, 
     ca: null, 
     cert: null, 
     key: null, 
     passPhrase: null, 
     rejectUnauthorized: false, 
     promoteLongs: true, 
     promoteValues: true, 
     promoteBuffers: false, 
     reconnect: true, 
     reconnectInterval: 1000, 
     reconnectTries: 30, 
     domainsEnabled: false, 
     disconnectHandler: [Object], 
     cursorFactory: [Object], 
     emitError: true, 
     socketOptions: {}, 
     clientInfo: [Object], 
     readPreference: [Object], 
     native_parser: true, 
     promiseLibrary: [Function: Promise], 
     bson: BSON {} }, 
    id: 0, 
    logger: Logger { className: 'Connection' }, 
    bson: BSON {}, 
    tag: undefined, 
    messageHandler: [Function], 
    maxBsonMessageSize: 67108864, 
    port: 27017, 
    host: 'localhost', 
    keepAlive: true, 
    keepAliveInitialDelay: 0, 
    noDelay: true, 
    connectionTimeout: 30000, 
    socketTimeout: 30000, 
    destroyed: false, 
    domainSocket: false, 
    singleBufferSerializtion: true, 
    serializationFunction: 'toBinUnified', 
    ca: null, 
    cert: null, 
    key: null, 
    passphrase: null, 
    ssl: false, 
    rejectUnauthorized: false, 
    checkServerIdentity: true, 
    responseOptions: 
     { promoteLongs: true, 
     promoteValues: true, 
     promoteBuffers: false }, 
    flushing: false, 
    queue: [], 
    connection: 
     Socket { 
     connecting: false, 
     _hadError: false, 
     _handle: [Object], 
     _parent: null, 
     _host: 'localhost', 
     _readableState: [Object], 
     readable: true, 
     domain: null, 
     _events: [Object], 
     _eventsCount: 8, 
     _maxListeners: undefined, 
     _writableState: [Object], 
     writable: true, 
     allowHalfOpen: false, 
     destroyed: false, 
     _bytesDispatched: 2134, 
     _sockname: null, 
     _pendingData: null, 
     _pendingEncoding: '', 
     server: null, 
     _server: null, 
     _idleTimeout: 30000, 
     _idleNext: [Object], 
     _idlePrev: [Object], 
     _idleStart: 3254, 
     read: [Function], 
     _consuming: true }, 
    writeStream: null, 
    hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c', 
    workItems: [ [Object] ], 
    buffer: null, 
    sizeOfMessage: 0, 
    bytesRead: 0, 
    stubBuffer: null }, 
    message: 
    Response { 
    parsed: true, 
    index: 126, 
    raw: <Buffer 7e 00 00 00 c0 90 00 00 03 00 00 00 01 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 5a 00 00 00 10 6e 00 01 00 00 00 10 6e 4d ... >, 
    data: <Buffer 7e 00 00 00 c0 90 00 00 03 00 00 00 01 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 5a 00 00 00 10 6e 00 01 00 00 00 10 6e 4d ... >, 
    bson: BSON {}, 
    opts: 
     { promoteLongs: true, 
     promoteValues: true, 
     promoteBuffers: false }, 
    length: 126, 
    requestId: 37056, 
    responseTo: 3, 
    responseFlags: 8, 
    cursorId: Long { _bsontype: 'Long', low_: 0, high_: 0 }, 
    startingFrom: 0, 
    numberReturned: 1, 
    documents: [ [Object] ], 
    cursorNotFound: false, 
    queryFailure: false, 
    shardConfigStale: false, 
    awaitCapable: true, 
    promoteLongs: true, 
    promoteValues: true, 
    promoteBuffers: false, 
    hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c' } } 

回答

0

collection.update()將只報告中所影響到它自己的回調文件的數量。

要檢索的文件,同時修改,你可以使用collection.findOneAndUpdate()collection.findAndModify()

collection.findOneAndUpdate:

db.collection.findOneAndUpdate(
    <filter>, 
    <update>, 
    { 
    projection: <document>, 
    sort: <document>, 
    maxTimeMS: <number>, 
    upsert: <boolean>, 
    returnNewDocument: <boolean> 
    } 
) 

Refrence:findOneAndUpdate

您的問題:

 db.test.findOneAndUpdate(
      {labId: lab_id}, 
      {$set:{version:nc.version}, 
      { 
      upsert: true, 
      returnNewDocument: true // use this key if wanted modified document 
      }, 
      function (error, result) { 

      if (error){ 
       // If there is error then 
       console.log("Error occured while adding in system"); 
      } 
      console.log('Result is ', result); 
     }) 
     ) 

collection.findOneAndModify:

db.collection.findAndModify({ 
    query: <document>, 
    sort: <document>, 
    remove: <boolean>, 
    update: <document>, 
    new: <boolean>, 
    fields: <document>, 
    upsert: <boolean>, 
    bypassDocumentValidation: <boolean>, 
    writeConcern: <document>, 
    collation: <document> 
}); 

參考:findOneAndModify:

注:使用findOneAndUpdate和findOneAndModify只有ü要更新單個文件,因爲多真不是它們提供。

+0

1.我只需要通過update添加文檔(upsert:true)來檢查重複是否不存在2.問題是關於objectID的問題,也就是我在哪裏得到了id_,之後的結果。 –

+0

U將在結果變量中獲得_id,並且上面的代碼將與更新一樣工作,我也給出了選項upsert true – Sam

+0

上面的示例適用於您的查詢,因爲您不使用{multi:true} – Sam