0

I have these models

var NotesSchema = new mongoose.Schema(
  {
    title: String, 
    category: [{ type: mongoose.ObjectId, ref: "Categories", default: [] }], 
  }
);
var CategoriesSchema = new Schema({
  name: { type: String, required: true }, 
  notes: [{ type: Schema.Types.ObjectId, ref: 'Notes' }]
});
var Notes = mongoose.model("Notes", NotesSchema);
var Cat = mongoose.model("Categories", CategoriesSchema);

I created the ability to add a note and the database get's populated. I post something like this

{ "title": "Post two", "categories": [ "tagOne", "tagTwo"  ] }

And the dummy final result in the DB is:

/* Notes Collection */
/* 1 */
{
    "_id" : ObjectId("5f7bd0ee7e59d9265a869427"),
    "category" : [ 
        ObjectId("5f7bd0ee7e59d9265a869428") //tagOne
    ],
    "title" : "Post One",
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5f7bd0fc7e59d9265a869429"),
    "category" : [ 
        ObjectId("5f7bd0ee7e59d9265a869428"), tagOne
        ObjectId("5f7bd0fc7e59d9265a86942a")  //tagTwo
    ],
    "title" : "Post two",
    "__v" : 0
}

And the Categories Collection

/* 1 */
{
    "_id" : ObjectId("5f7bd0ee7e59d9265a869428"),
    "notes" : [ 
        ObjectId("5f7bd0ee7e59d9265a869427"), 
        ObjectId("5f7bd0fc7e59d9265a869429")
    ],
    "name" : "tagOne",
    "__v" : 1
}

/* 2 */
{
    "_id" : ObjectId("5f7bd0fc7e59d9265a86942a"),
    "notes" : [ 
        ObjectId("5f7bd0fc7e59d9265a869429")
    ],
    "name" : "tagTwo",
    "__v" : 0
}

Say I want to remove tagOne from Post two. I can easily find Post two and remove the id of tagOne from the array list. But at the same time I'd need to go to tagOne and remove the id of Post two.

Above I have Post two, it has two categories, If I want to remove one how the tags from the post, how to I remove it from the note and remove the note from category

Action: remove "category1" from "post one", AND "post one" from "category1"

relidon
  • 2,142
  • 4
  • 21
  • 37

0 Answers0