簡體   English   中英

Mongoose 架構內的相同架構

[英]Same Schema within Schema in Mongoose

var dirSchema = new mongoose.Schema([{type:String, 
                name:String, path:String, children:[dirSchema]}]);

我想在 mongodb 中使用 nodejs 存儲這種類型的模式嗎? 它就像一個文件系統結構。 如果我有一個路徑說 /foo/bar/hello/123,我如何查詢集合。

我不記得我當時是如何解決它的(2014 年),但可以通過使用圖形查找操作來解決。 深度字段可用於識別文件夾的深度。

db.dirs.aggregate([ 
    { $match: {
      dirId: "foo"
    } }, 
    {$graphLookup:{
    from:"product", 
    startWith:"$_id", 
    connectFromField:"_id", 
    connectToField:"parentId",
    "depthField": "depth" 
    as:"children"
  }}])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM