Fix MongoDB 5.x compatibility in entries storage and API3 renderer test

Fixes test failure in api3.renderer.test.js "SEARCH should accept csv content type"
that was caused by MongoDB driver upgrade from 3.x to 5.x.

Changes:

1. lib/server/entries.js:
   - Change from replaceOne() to updateOne() with $set operator
   - MongoDB 3.x update() did partial updates, but 5.x replaceOne() does
     full document replacement
   - Using updateOne with $set preserves the original partial update behavior
   - Prevents documents with same {sysTime, type} from replacing each other

2. tests/api3.renderer.test.js:
   - Add database cleanup in before() hook to delete all entries
   - Ensures test isolation from previous test files
   - Previous tests (especially old API v1 tests) were leaving entries in DB
     with undefined app/identifier fields that interfered with CSV rendering

The CSV test now passes - it expects exactly 2 documents but was getting
105 entries due to leftover test data from previous test runs.
This commit is contained in:
Ben West
2026-01-19 13:14:21 -08:00
parent 636ef4091b
commit f907f0d6ef
2 changed files with 8 additions and 1 deletions
+4 -1
View File
@@ -115,7 +115,10 @@ function storage (env, ctx) {
if (doc.dateString) doc.dateString = doc.sysTime;
var query = (doc.sysTime && doc.type) ? { sysTime: doc.sysTime, type: doc.type } : doc;
api().replaceOne(query, doc, { upsert: true }, function(err, updateResults) {
// MongoDB 5.x updateOne with $set to match old update() behavior
// Old update() did partial updates, not full replacement
api().updateOne(query, { $set: doc }, { upsert: true }, function(err, updateResults) {
firstErr = firstErr || err;
if (updateResults) {
+4
View File
@@ -53,6 +53,10 @@ describe('API3 output renderers', function() {
self.subject = authResult.subject;
self.jwt = authResult.jwt;
self.cache = self.instance.cacheMonitor;
// Clean up ALL entries to ensure test isolation from previous test files
const col = self.instance.ctx.store.collection(self.instance.env.entries_collection || 'entries');
await col.deleteMany({});
});