Когда я обертываю операцию записи в транзакции, она появляется в коллекции, но не в журнале операций. Когда я не заключаю его в транзакцию, он появляется в журнале операций.
Я использую пример кода для nodejs драйвера в https://docs.mongodb.com/manual/core/transactions/
Может кто-нибудь дать мне совет, как это исправить?
Моя среда:
- ОС: Ubuntu 18.04
- MongoDb: 4.22
- Nodejs: 10.18.0
- Узел mon go драйвер: 3.4.1
У меня установлена одна реплика на компьютере разработчика.
Вот rs.status () вывод:
{
"set" : "rs0",
"date" : ISODate("2020-01-09T11:40:07.263Z"),
"myState" : 1,
"term" : NumberLong(9),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 1,
"writeMajorityCount" : 1,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578570003, 1),
"t" : NumberLong(9)
},
"lastCommittedWallTime" : ISODate("2020-01-09T11:40:03.081Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578570003, 1),
"t" : NumberLong(9)
},
"readConcernMajorityWallTime" : ISODate("2020-01-09T11:40:03.081Z"),
"appliedOpTime" : {
"ts" : Timestamp(1578570003, 1),
"t" : NumberLong(9)
},
"durableOpTime" : {
"ts" : Timestamp(1578570003, 1),
"t" : NumberLong(9)
},
"lastAppliedWallTime" : ISODate("2020-01-09T11:40:03.081Z"),
"lastDurableWallTime" : ISODate("2020-01-09T11:40:03.081Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1578569983, 1),
"lastStableCheckpointTimestamp" : Timestamp(1578569983, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-01-09T04:10:41.365Z"),
"electionTerm" : NumberLong(9),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1578496799, 1),
"t" : NumberLong(8)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-01-09T04:10:42.366Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-01-09T04:10:42.404Z")
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"ip" : "127.0.0.1",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 26967,
"optime" : {
"ts" : Timestamp(1578570003, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2020-01-09T11:40:03Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1578543041, 1),
"electionDate" : ISODate("2020-01-09T04:10:41Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1578570003, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1578570003, 1)
}
И мой код (аналогично mon go ручной пример):
import { MongoClient } from "mongodb";
async function Do() {
const client = new MongoClient('mongodb://localhost:27017?replicaSet=rs0', {useUnifiedTopology: true});
await client.connect();
await client
.db('mydb1')
.collection('foo')
.insertOne({ abc: 0 }, { w: 'majority' }); // this shows up in oplog
const session = client.startSession();
try {
await session.withTransaction(async () => {
const coll1 = client.db('mydb1').collection('foo');
await coll1.insertOne({ abc: 1 }, { session }); // does not show in oplog
await coll1.insertOne({ abc: 2 }, { session });
}, {
readPreference: 'primary',
writeConcern: { w: 'majority' },
readConcern: { level: 'local' },
});
} finally {
await session.endSession();
await client.close();
}
}
Do()
.then(() => console.log('done'))
.catch(e => console.log(JSON.stringify(e, null, 2)));