首先。嘗試使用從每個異步回調返回的「err」對象的概念時,不使用try catch塊進行編碼。您可以在回調中處理錯誤。 「拋出」錯誤還會阻止其他電子郵件被更新。但也許這就是你想要的。現在並不重要。
現在,回到你的問題。讓我告訴你什麼result
是,在這個例子:
// inserting.
collection.insertOne({_id: "lala", a:10, b:20}, function (err, r) {
assert.equal(null, err);
assert.equal(1, r.insertedCount);
// updating.
collection.updateOne({_id: "lala"}, {$set: {a: 99}}, {}, function(err, result) {
assert.equal(null, err);
console.log(result) // first thing printed.
console.log(JSON.stringify(result, null, "\t")) // second print.
// checking whats inside it.
collection.findOne({_id: "lala"}, {}, function(err, doc) {
assert.equal(null, err);
console.log(doc) // third print.
db.close() // don't close connection if you don't need to.
})
})
})
3件印刷事情會(滾動到底看到你真正想要什麼):
CommandResult {
result: { ok: 1, nModified: 1, n: 1 },
connection:
Connection {
domain: null,
_events:
{ close: [Object],
error: [Object],
timeout: [Object],
parseError: [Object],
connect: [Function] },
_eventsCount: 5,
_maxListeners: undefined,
options:
{ socketOptions: {},
auto_reconnect: true,
host: 'localhost',
port: 27017,
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
disconnectHandler: [Object],
bson: BSON {},
messageHandler: [Function],
wireProtocolHandler: [Object] },
id: 1,
logger: Logger { className: 'Connection' },
bson: BSON {},
tag: undefined,
messageHandler: [Function],
maxBsonMessageSize: 67108864,
port: 27017,
host: 'localhost',
keepAlive: true,
keepAliveInitialDelay: 0,
noDelay: true,
connectionTimeout: 0,
socketTimeout: 0,
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 },
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: 334,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1,
read: [Function],
_consuming: true },
writeStream: null,
hashedName: '29bafad3b32b11dc7ce934204952515ea5984b3c',
buffer: null,
sizeOfMessage: 0,
bytesRead: 0,
stubBuffer: null },
matchedCount: 1,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0 }
{
"ok": 1,
"nModified": 1,
"n": 1
}
{ _id: 'lala', a: 99, b: 20 }
的第一個對象是result
從「updateOne()」返回。 第二個對象是result
的「toString()」實現,這就是result.result
(回滾到頂部)的內容。 最後一個對象是查詢更新後的文檔後得到的。
你可以在nodejs網站上找到更多關於本地mongodb驅動程序的示例:http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#updateOne 它們充滿了示例。
D_emails是一個字典,其中包含密鑰和mongo文檔的電子郵件,用於標識其值。我這樣做是爲了確保電子郵件中沒有重複。 – GoofyFoot
'results'的值是多少? – abdulbarik