1.4.1. fejezet, MongoDB

Kapcsolódó hivatkozások

SpringBoot SwaggerUI POST DBRef paraméterezés

{
  "path": "/home",
  "createdAt": "2025-04-11T12:07:58.710Z",
  "parentDirectory": { "id": "67fcac477a05e302e68efc18" }
}

Lekérdezések a MongoDB shell-ben

db.fileInfo.find({"directory":DBRef("directory",ObjectId("67f8b918c7e849395771a0d7"))})
 
# Like
db.fileInfo.find({"name":{"$regex":".*gép$"}})
 
db.directory.insertOne({
    "_id": ObjectId(),
    "path": "/home/pzoli"
})
 
const newParentId = ObjectId('67fd06fd1c211336e81164dd')
const parentCollectionName = "directory";
 
db.directory.updateOne(
  {_id: ObjectId('67fcac477a05e302e68efc18')},
  {$set: {'parentDirectory':{'$ref':parentCollectionName,'_id':newParentId}}},
  {$unset: ["createdAt"]},
)
 
db.direcory.deleteOne(
  {_id: ObjectId('67f8e49a2b7bc08f75d9eabf')}
)
 
db.proxy.createIndex( { "email" : 1 }, { unique : true } )

Gráf leképezések

Könyvtár hierachiából útvonal generálása:

[
  {
    $graphLookup: {
      from: "directory",
      startWith: "$parentDirectory.$id",
      connectFromField: "parentDirectory.$id",
      connectToField: "_id",
      as: "parents",
      // RECOMMENDED: Add depthField for reliable sorting later
      depthField: "level"
    }
  },
  {
    // Optional but RECOMMENDED: Sort parents from root to immediate parent
    // $graphLookup doesn't guarantee order, sorting by level fixes this.
    $addFields: {
      parents: {
        $sortArray: {
          input: "$parents",
          sortBy: {
            level: -1
          }
        } // Root (highest level) first
      }
    }
  },
  {
    $project: {
      _id: 1,
      // Keep the original _id
      originalPathField: "$path",
      // Keep the original path value if needed
      // Assign the result of $reduce to a field, e.g., 'constructedPath'
      constructedPath: {
        $reduce: {
          input: "$parents.path",
          // Iterate over the 'path' field of each parent
          initialValue: "",
          // Start with an empty string
          in: {
            // Basic concatenation - BEWARE of potential double slashes if paths start with /
            // and the root path might be missing the leading slash here.
            $concat: [
              "$$value",
              {
                $cond: {
                  if: {
                    $eq: ["$$value", ""]
                  },
                  then: "",
                  else: "/"
                }
              },
              // Add '/' unless it's the first part
              "$$this"
            ]
          }
        }
      },
      // You might want the full path including the current document's path:
      fullPathAttempt: {
        $let: {
          // Use $let to calculate intermediate path
          vars: {
            ancestorPath: {
              // Calculate ancestor path first
              $reduce: {
                input: "$parents.path",
                initialValue: "",
                in: {
                  $concat: [
                    "$$value",
                    {
                      $cond: {
                        if: {
                          $eq: ["$$value", ""]
                        },
                        then: "",
                        else: "/"
                      }
                    },
                    "$$this"
                  ]
                }
              }
            }
          },
          in: {
            // Combine ancestor path with current document's path
            $concat: [
              "$$ancestorPath",
              {
                $cond: {
                  if: {
                    $eq: ["$$ancestorPath", ""]
                  },
                  then: "",
                  else: "/"
                }
              },
              "$path" // Append the current document's path
            ]
          }
        }
      }
    }
  }
]