Skip to content

Commit f5a77fb

Browse files
Copilotcubap
andcommitted
Optimize queries to use _id instead of @id for root object lookups
Co-authored-by: cubap <1119165+cubap@users.noreply.github.com>
1 parent d165582 commit f5a77fb

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

controllers/delete.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,14 @@ async function newTreePrime(obj) {
221221
async function getAllVersions(obj) {
222222
let ls_versions
223223
let primeID = obj?.__rerum.history.prime
224-
let rootObj = ( primeID === "root")
225-
? JSON.parse(JSON.stringify(obj))
226-
: await db.findOne({ "@id": primeID })
224+
let rootObj
225+
if (primeID === "root") {
226+
rootObj = JSON.parse(JSON.stringify(obj))
227+
} else {
228+
//Use _id for indexed query performance instead of @id
229+
const primeHexId = parseDocumentID(primeID)
230+
rootObj = await db.findOne({"$or":[{"_id": primeHexId}, {"__rerum.slug": primeHexId}]})
231+
}
227232
ls_versions = await db.find({ "__rerum.history.prime": rootObj['@id'] }).toArray()
228233
ls_versions.unshift(rootObj)
229234
return ls_versions

controllers/utils.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,16 @@ async function alterHistoryNext(objToUpdate, newNextID) {
198198
async function getAllVersions(obj) {
199199
let ls_versions
200200
let primeID = obj?.__rerum.history.prime
201-
let rootObj = ( primeID === "root")
202-
? //The obj passed in is root. So it is the rootObj we need.
203-
JSON.parse(JSON.stringify(obj))
204-
: //The obj passed in knows the ID of root, grab it from Mongo
205-
await db.findOne({ "@id": primeID })
206-
/**
207-
* Note that if you attempt the following code, it will cause Cannot convert undefined or null to object in getAllVersions.
208-
* rootObj = await db.findOne({"$or":[{"_id": primeID}, {"__rerum.slug": primeID}]})
209-
* This is the because some of the @ids have different RERUM URL patterns on them.
210-
**/
201+
let rootObj
202+
if (primeID === "root") {
203+
//The obj passed in is root. So it is the rootObj we need.
204+
rootObj = JSON.parse(JSON.stringify(obj))
205+
} else {
206+
//The obj passed in knows the ID of root, grab it from Mongo
207+
//Use _id for indexed query performance instead of @id
208+
const primeHexId = parseDocumentID(primeID)
209+
rootObj = await db.findOne({"$or":[{"_id": primeHexId}, {"__rerum.slug": primeHexId}]})
210+
}
211211
//All the children of this object will have its @id in __rerum.history.prime
212212
ls_versions = await db.find({ "__rerum.history.prime": rootObj['@id'] }).toArray()
213213
//The root object is a version, prepend it in

0 commit comments

Comments
 (0)