简体   繁体   中英

How to get the connected objects within a collection in an array in Mongoose MongoDb NodeJS

I am new to NodeJS and I am using NodeJS Framework HapiJs. I have parent-child schema of a Model "Elements" in which parent and child belongs to the same collection. The schema is similar to following:

var mschema = new Schema({
    autoIncrementId: { type: Number,unique: true,index:true,sparse:true},
    elementName: {
        type: String,
        required: true
    },
    elementCanvasId: {
        type: String,
        required: true
    },
    parentId: {
        type: Schema.Types.ObjectId,
        required: false,
        ref: "Elements",
    },
    parentCanvasId: {
        type: String,
        required: false
    },
});

var modules = Mongoose.model('Elements', mschema);

here elementCanvasId is generated at client side. And parentCanvasId is a reference to elementCanvasId of the parent element. Also its not necessary that every element will have a parent. Now based on elementCanvasId of element I wanna fetch all the elements that comes under hierarchy(ie all its children elements and then children's subchildren and then their's subchildren and so on.) of that element in a Single Dimensional Array. I cannot use Schema.Types.ObjectId for this purspose as I only have to use elementCanvasId which is generated at client's side only. So, can someone help me out to get it done. Any kind of help is hugely appreciated. Thanks in advance.

I think what you are looking for is $graphLookup , however it can only be used from version 3.4+, it gets quite complicated

For full details see https://docs.mongodb.com/v3.4/reference/operator/aggregation/graphLookup/

Elements.aggregate( [
   {
      $graphLookup: {
         from: "elements",
         startWith: "$parentCanvasId",
         connectFromField: "parentCanvasId",
         connectToField: "elementCanvasId",
         as: "elementHierarchy"
      }
   }
] )

You need to use $graphLookup . As $graphLookup performs a recursive search on a collection.

Elements.aggregate([{ $graphLookup : {
  from : 'elements', // MongoDB collection name not mongoose schema name
  startWith : '$parentCanvasId', //it is expression which can include can include field paths and system variables, literals, expression objects etc.
  connectFromField : 'elementCanvasId',
  as : 'children'
} }])

If you want to fetch element hierarchy for only one element then you need to use $match in aggregation pipeline

Elements.aggregate([{ $match : { autoIncrementId : '1'  } }, { $graphLookup : {
  from : 'elements', // MongoDB collection name not mongoose schema name
  startWith : '$parentCanvasId', 
  connectFromField : 'elementCanvasId',
  as : 'children'
} }])

Note : autoIncrementId : 1 is the condition by which you can fetch the result for single element

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