Monday, 1 April 2019

Mongoose: Update all children in multiply nested documents?

I have a scheme I'll just call a Node, that can be nested within itself as a child creating a structure like Node1>Node2>Node3, like this:const Node = new Schema({ parentNode: {type: Schema.Types.ObjectId, ref: "Node"}, nodeName: { type: String, required: true }, clicks: Number active: Boolean, subNodes: [{type: Schema.Types.ObjectId, ref: "Node"}] }); As you can see you can set a node to active or not and can track clicks. In the example Node1>Node2>Node3, if I set Node1 to inactive I'd like Node2 and Node3 to be inactive as well. Similarly, if I try to add a click to Node3, but Node1 is inactive (and thus Node3 must be inactive) I don't want to add the click.​Would it be better practice to, when set inactive, set all subchildren as inactive. Or would it be better to, on click add, simply check if all parents are active before adding it? Also short of recursing through all subnodes is there a Mongoose function to update all children and subchildren to inactive in one call?

Submitted April 01, 2019 at 11:47AM by ReactiveNative

No comments:

Post a Comment