Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

mongodb - Mongo Query for document existence in two collections

I am new to Mongo and need to write a query which will perform the following steps:

  1. Read collection "STAGING" and get all documents where "state" = "COMPLETE"
  2. For each record found in step (1), check second collection "PROCESS" for its existence
  3. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...