当通过mongoskin插入mongodb时,我可以通过以下方式获得对象id。这是非常好的工作
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:true}进行更新时,我找不到任何内容
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(即在mongodb中添加对象后得到的_id)
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 :(得分:0)
collection.update()
只会报告受其回调影响的文档数量。
要在修改时检索文档,您可以使用collection.findOneAndUpdate()
或collection.findAndModify()
collection.findOneAndUpdate:
db.collection.findOneAndUpdate(
<filter>,
<update>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>
}
)
问题:
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>
});
注意: 仅当您想要更新单个文档时才使用findOneAndUpdate和findOneAndModify,因为其中没有多个true。