简体   繁体   English

Mongoose 架构内的相同架构

[英]Same Schema within Schema in Mongoose

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

I want to store this type of schema using nodejs in mongodb?我想在 mongodb 中使用 nodejs 存储这种类型的模式吗? It is just like a file system structure.它就像一个文件系统结构。 How do i query into the collections if i have a path say /foo/bar/hello/123.如果我有一个路径说 /foo/bar/hello/123,我如何查询集合。

I don't remember how I solved it then (2014) but it can be solved by using Graph lookup operation.我不记得我当时是如何解决它的(2014 年),但可以通过使用图形查找操作来解决。 The depth field can be used to identify the depth of the folder.深度字段可用于识别文件夹的深度。

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