gpt4 book ai didi

node.js - 更新命令 mongoskin 后的对象 ID

转载 作者:太空宇宙 更新时间:2023-11-04 02:05:44 29 4
gpt4 key购买 nike

当通过 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' } }

最佳答案

collection.update() 只会报告受到其自身回调影响的文档数量。

要在修改时检索文档,可以使用collection.findOneAndUpdate()collection.findAndModify()

collection.findOneAndUpdate:

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

引用: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,因为 multi true 在其中不可用。

关于node.js - 更新命令 mongoskin 后的对象 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44401821/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com