0

In Node and Mongoose, I'd like to remove an object from an array in a document. The structure is like this.

{ 
    _id: ObjectId,
    title: String,
    tags: [
        { text: String },
        { text: String }
    ]
}

I will look up an item by it's _id, but then I want to look within the tags for a certain String and remove that from the array.

1
  • 1
    And? Where are you stuck? What have you tried? Commented Mar 4, 2015 at 8:45

1 Answer 1

1

You effectively want to update the document and use the $pull operator with a query matching the matching value under "tags.tag":

Model.update(
    { "_id": docId, "tags.tag": "mytag" },
    { "$pull": { "tags": { "tag": "mytag" } },
    function(err,numAffected) {

    }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.