Currently, I am in the process of developing a code that involves searching through numerous objects stored in a MongoDB database. The main objective is to retrieve these objects based on their respective IDs and then further investigate the ID references within each object. Essentially, the program needs to focus on locating a specific ID by initially fetching the object related to that ID, followed by exploring the associated IDs within the object.
async function objectIdentifier(ID1, ID2, depth, previousList = []) {
let pathway = []
if (ID1 == ID2) {
return [ID2]
} else {
previousList.push(ID1)
let obj1 = await findObjectByID(ID1)
let connectedID = obj1.connections.concat(obj1.inclusions) //creates an array comprising both object references and referenced objects
let mapPromises = connectedID.map(async (id) => {
return findID(id) //async function
})
let fulfilled = await Promise.allSettled(mapPromises)
let list = fulfilled.map((object) => {
return object.value.main, object.value.included
})
list = list.filter(id => !previousList.includes(id))
for (id of list) {
await objectIdentifier(id, ID2, depth - 1, previousList).then(result => {
pathway = [ID1].concat(result)
if (pathway[pathway.length - 1] == ID2) {
return pathway
}})
}
}
if (pathway[pathway.length - 1] == ID2) {
return pathway
}
}
I am exploring ways to enhance my code so that it functions similar to a tree search method, where each object and its corresponding ID represent individual nodes within the search structure.