【发布时间】:2021-03-12 22:36:35
【问题描述】:
我是 MongoDB 的初学者。我将它与节点和快递一起使用。我的目标是“插入”(或者说“upsert”更好地说)一个在网页上注册的新用户,该用户使用数据库函数在插入时创建“registration_time”。其他人告诉我, updateOne() 而不是 insert() 是实现这一目标的最佳方式。也许那是个坏建议。所以我有下面的代码,它给出了错误“未知的顶级运算符 $set”。好的,如果这不起作用,我想我会做一些涉及 _id 和 new ObjectID() 的事情来让它工作。
const registerUser = async (user) => {
const database = await connect();
return database.collection('user').updateOne(
{
$set: {
username: user.username,
email: user.email,
password: user.password,
password_hash: user.password_hash,
},
},
{ $currentDate: { registration_time: true } },
{ $upsert: true }
);
};
然后我将代码更改为下面的代码(让 Mongo 在插入时给我一个 ObjectID),它消除了未知的顶级运算符 $set" 错误,但给了我一些我没有的其他错误知道如何解释。
const registerUser = async (user) => {
const database = await connect();
return database.collection('user').updateOne(
{ _id: new ObjectID() },
{
$set: {
username: user.username,
email: user.email,
password: user.password,
password_hash: user.password_hash,
},
},
{ $currentDate: { registration_time: true } },
{ $upsert: true }
);
};
我在我的网页上确认没有插入新用户。这是错误对象(我假设就是这样)。我做错了什么?
app:routes:account CommandResult {
app:routes:account result: {
app:routes:account n: 0,
app:routes:account nModified: 0,
app:routes:account opTime: { ts: [Timestamp], t: 2 },
app:routes:account electionId: 7fffffff0000000000000002,
app:routes:account ok: 1,
app:routes:account '$clusterTime': { clusterTime: [Timestamp], signature: [Object] },
app:routes:account operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1606781586 }
app:routes:account },
app:routes:account connection: Connection {
app:routes:account _events: [Object: null prototype] {
app:routes:account commandStarted: [Function],
app:routes:account commandFailed: [Function],
app:routes:account commandSucceeded: [Function],
app:routes:account clusterTimeReceived: [Function]
app:routes:account },
app:routes:account _eventsCount: 4,
app:routes:account _maxListeners: undefined,
app:routes:account id: 1,
app:routes:account address: '34.123.254.59:27017',
app:routes:account bson: BSON {},
app:routes:account socketTimeout: 0,
app:routes:account host: 'cluster0-shard-00-02.ht4ef.mongodb.net',
app:routes:account port: 27017,
app:routes:account monitorCommands: false,
app:routes:account closed: false,
app:routes:account destroyed: false,
app:routes:account lastIsMasterMS: 24,
app:routes:account [Symbol(kCapture)]: false,
app:routes:account [Symbol(description)]: StreamDescription {
app:routes:account address: '34.123.254.59:27017',
app:routes:account type: 'RSPrimary',
app:routes:account minWireVersion: 0,
app:routes:account maxWireVersion: 8,
app:routes:account maxBsonObjectSize: 16777216,
app:routes:account maxMessageSizeBytes: 48000000,
app:routes:account maxWriteBatchSize: 100000,
app:routes:account compressors: []
app:routes:account },
app:routes:account [Symbol(generation)]: 0,
app:routes:account [Symbol(lastUseTime)]: 500832082,
app:routes:account [Symbol(queue)]: Map {},
app:routes:account [Symbol(messageStream)]: MessageStream {
app:routes:account _readableState: [ReadableState],
app:routes:account readable: true,
app:routes:account _events: [Object: null prototype],
app:routes:account _eventsCount: 7,
app:routes:account _maxListeners: undefined,
app:routes:account _writableState: [WritableState],
app:routes:account writable: true,
app:routes:account allowHalfOpen: true,
app:routes:account bson: BSON {},
app:routes:account maxBsonMessageSize: 67108864,
app:routes:account [Symbol(kCapture)]: false,
app:routes:account [Symbol(buffer)]: [BufferList]
app:routes:account },
app:routes:account [Symbol(stream)]: TLSSocket {
app:routes:account _tlsOptions: [Object],
app:routes:account _secureEstablished: true,
app:routes:account _securePending: false,
app:routes:account _newSessionPending: false,
app:routes:account _controlReleased: true,
app:routes:account secureConnecting: true,
app:routes:account _SNICallback: null,
app:routes:account servername: 'cluster0-shard-00-02.ht4ef.mongodb.net',
app:routes:account alpnProtocol: false,
app:routes:account authorized: true,
app:routes:account authorizationError: null,
app:routes:account encrypted: true,
app:routes:account _events: [Object: null prototype],
app:routes:account _eventsCount: 9,
app:routes:account connecting: false,
app:routes:account _hadError: false,
app:routes:account _parent: null,
app:routes:account _host: 'cluster0-shard-00-02.ht4ef.mongodb.net',
app:routes:account _readableState: [ReadableState],
app:routes:account readable: true,
app:routes:account _maxListeners: undefined,
app:routes:account _writableState: [WritableState],
app:routes:account writable: true,
app:routes:account allowHalfOpen: false,
app:routes:account _sockname: null,
app:routes:account _pendingData: null,
app:routes:account _pendingEncoding: '',
app:routes:account server: undefined,
app:routes:account _server: null,
app:routes:account ssl: [TLSWrap],
app:routes:account _requestCert: true,
app:routes:account _rejectUnauthorized: true,
app:routes:account timeout: 0,
app:routes:account _peername: [Object],
app:routes:account [Symbol(res)]: [TLSWrap],
app:routes:account [Symbol(verified)]: true,
app:routes:account [Symbol(pendingSession)]: null,
app:routes:account [Symbol(asyncId)]: 438,
app:routes:account [Symbol(kHandle)]: [TLSWrap],
app:routes:account [Symbol(kSetNoDelay)]: true,
app:routes:account [Symbol(lastWriteQueueSize)]: 0,
app:routes:account [Symbol(timeout)]: Timeout {
app:routes:account _idleTimeout: -1,
app:routes:account _idlePrev: null,
app:routes:account _idleNext: null,
app:routes:account _idleStart: 23841,
app:routes:account _onTimeout: null,
app:routes:account _timerArgs: undefined,
app:routes:account _repeat: null,
app:routes:account _destroyed: true,
app:routes:account [Symbol(refed)]: false,
app:routes:account [Symbol(asyncId)]: 441,
app:routes:account [Symbol(triggerId)]: 433
app:routes:account },
app:routes:account [Symbol(kBuffer)]: null,
app:routes:account [Symbol(kBufferCb)]: null,
app:routes:account [Symbol(kBufferGen)]: null,
app:routes:account [Symbol(kCapture)]: false,
app:routes:account [Symbol(kBytesRead)]: 0,
app:routes:account [Symbol(kBytesWritten)]: 0,
app:routes:account [Symbol(connect-options)]: [Object],
app:routes:account [Symbol(disable-renegotiation)]: true
app:routes:account },
app:routes:account [Symbol(clusterTime)]: { clusterTime: [Timestamp], signature: [Object] },
app:routes:account [Symbol(ismaster)]: {
app:routes:account hosts: [Array],
app:routes:account setName: 'atlas-qzzelf-shard-0',
app:routes:account setVersion: 1,
app:routes:account ismaster: true,
app:routes:account secondary: false,
app:routes:account primary: 'cluster0-shard-00-02.ht4ef.mongodb.net:27017',
app:routes:account tags: [Object],
app:routes:account me: 'cluster0-shard-00-02.ht4ef.mongodb.net:27017',
app:routes:account electionId: 7fffffff0000000000000002,
app:routes:account lastWrite: [Object],
app:routes:account maxBsonObjectSize: 16777216,
app:routes:account maxMessageSizeBytes: 48000000,
app:routes:account maxWriteBatchSize: 100000,
app:routes:account localTime: 2020-12-01T00:13:06.393Z,
app:routes:account logicalSessionTimeoutMinutes: 30,
app:routes:account connectionId: 590075,
app:routes:account minWireVersion: 0,
app:routes:account maxWireVersion: 8,
app:routes:account readOnly: false,
app:routes:account ok: 1,
app:routes:account '$clusterTime': [Object],
app:routes:account operationTime: [Timestamp]
app:routes:account }
app:routes:account },
app:routes:account message: BinMsg {
app:routes:account parsed: true,
app:routes:account raw: <Buffer f5 00 00 00 7c 9a 39 06 06 00 00 00 dd 07 00 00 00 00 00 00 00 e0 00 00 00 10 6e 00 00 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 00 00 00 00 03 6f 70 ... 195 more bytes>,
app:routes:account data: <Buffer 00 00 00 00 00 e0 00 00 00 10 6e 00 00 00 00 00 10 6e 4d 6f 64 69 66 69 65 64 00 00 00 00 00 03 6f 70 54 69 6d 65 00 1c 00 00 00 11 74 73 00 02 00 00 ... 179 more bytes>,
app:routes:account bson: BSON {},
app:routes:account opts: { promoteLongs: true, promoteValues: true, promoteBuffers: false },
app:routes:account length: 245,
app:routes:account requestId: 104438396,
app:routes:account responseTo: 6,
app:routes:account opCode: 2013,
app:routes:account fromCompressed: undefined,
app:routes:account responseFlags: 0,
app:routes:account checksumPresent: false,
app:routes:account moreToCome: false,
app:routes:account exhaustAllowed: false,
app:routes:account promoteLongs: true,
app:routes:account promoteValues: true,
app:routes:account promoteBuffers: false,
app:routes:account documents: [ [Object] ],
app:routes:account index: 229
app:routes:account },
app:routes:account modifiedCount: 0,
app:routes:account upsertedId: null,
app:routes:account upsertedCount: 0,
app:routes:account matchedCount: 0
app:routes:account } +1ms
【问题讨论】:
标签: javascript node.js database mongodb upsert