It is possible to add new field into existing collection which connected each others?

I have a collection “documents” and “request_documents”.
I want to check “members” which has access on “documents” from “request_documents”

documents
_id
folderName

 -------------------------

request_document: 
_id
documentId
memberId
isAccept
deny

so when user want to “Get” data to “documents”, query gonna check is member be able to access the document if he/she in the “request_document”, if that member “_id” available on the “request_document”, then the respond on document gonna has this:

documents
_id
folderName
hasAccess 

so on “hasAccess” gonna has logic like this => if member _id exist in collection “request_document” and “isAccept” true, then “hasAccess” gonna be true , and if exist but isAccept and deny false, then “waiting”, otherwise “false”

is that possible to do that and how to I added new fields for that??

Hi @Virtual_Database - Have you resolved this or this still an open question? If it’s an open question, can you update it with a concrete example? I’m having trouble understanding the question.

Hello : )

I am not sure what you need,but the code below does that.

documents
_id
folderName

request_document: 
_id
documentId
memberId
isAccept
deny

Call
function get(memberID,documentID)

To get the response document
_id
folderName
hasAccess


hasAcces value calculated
  if memberID is not in request_document Collection
    "false"
  else
    if isAccept
      "true"
    else if not deny
      "waiting"      
    else "false"

The first solution dont use a subquery,and issues 2 queries.
The second uses facet that allows subquery.

GET(memberID,documentID)
{

  var isMemberInRequest? = query,filter request_document for that memberId (match stage) 
  if not isMemberInRequest?
     aggregation pipeline on documents
     filter request_document for that documentID (match stage)
     add field access that will be false (addFields stage)
  else aggregation pipeline on request_document
       filter request_document for that memberId (match stage)
       add field access (addFields access value)
       access value (use aggregation operators and if etc)
         if (isAccept) true 
         else if (isAccept && deny false => waiting) waiting
         else false 
       project documentId access
       join(lookup) request_document documents on documentId 
       project _id folderName hasAccess
}
GET(memberID,documentID)
{
  
  aggregation pipeline on documents
  filter documents for that documentID (match stage)
  facet 
    "access" join(lookup) with pipeline on request_document
             calculate access 
             addfield access
  addFields 
     access if (empty the above acess(zero joined,member was not on request_document))
            false
            else the access i calculated on facet stage
}

The pipeline for the second could be like that.(i used sql like join with unwind and replace root)
aDocumentID,and aMemberID ,would be the function arguments

[
    {
      "$match": {
        "$expr": {
          "$eq": [
            "$docuemntId",
            "aDocumentID"
          ]
        }
      }
    },
    {
      "$facet": {
        "access": [
          {
            "$lookup": {
              "from": "request_document",
              "localField": "_id",
              "foreignField": "documentId",
              "as": "joined__"
            }
          },
          {
            "$unwind": {
              "path": "$joined__"
            }
          },
          {
            "$replaceRoot": {
              "newRoot": {
                "$mergeObjects": [
                  "$joined__",
                  "$$ROOT"
                ]
              }
            }
          },
          {
            "$project": {
              "joined__": 0
            }
          },
          {
            "$match": {
              "$expr": {
                "$eq": [
                  "$memberId",
                  "aMemberID"
                ]
              }
            }
          },
          {
            "$addFields": {
              "accessValue": {
                "$cond": [
                  "$isAccept",
                  {
                    "$cond": [
                      {
                        "$not": [
                          "$deny"
                        ]
                      },
                      "waiting",
                      "true"
                    ]
                  },
                  "true"
                ]
              }
            }
          }
        ]
      }
    },
    {
      "$addFields": {
        "access": {
          "$cond": [
            {
              "$eq": [
                {
                  "$size": "$access"
                },
                0
              ]
            },
            "false",
            "$access.accessValue"
          ]
        }
      }
    }
  ]

I havent tested it with values,my answer is not complete,but might help.

My apologies - I still don’t understand the question. Perhaps someone else will. Adding a sample document for each collection as well as your expected output might help.