简体   繁体   中英

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? 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.

I don't remember how I solved it then (2014) but it can be solved by using Graph lookup operation. 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"
  }}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM