I am new to Mongo and need to write a query which will perform the following steps:
- Read collection "STAGING" and get all documents where "state" = "COMPLETE"
- For each record found in step (1), check second collection "PROCESS" for its existence
- If document exists in "PROCESS", do nothing. If document does not exist, insert a document.
I've tried to script the query up, but I'm hitting a wall. Here is my attempt so far:-
db.staging.find({"state":"COMPLETE"}).forEach(
stagingDoc => {
if (null !== db.process.find({ "jobReference": stagingDoc.jobReference }).forEach(
record => {
db.process.insertOne({
"createdOn": new Date(),
"retryCount": 0,
"jobReference": record.jobReference,
...
})
}
))
question from:
https://stackoverflow.com/questions/65941322/mongo-query-for-document-existence-in-two-collections 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…