Q:

Remove sub-document from MongoDB if you don't know sub-document name

How does one unset a sub-document from MongoDB if one doesn't know the specific name of the sub-document?
In otherwords, I'd like to identify the sub-document based on some or another value for its fields...however, the actual name of the sub-document isn't known. So I'd like to somehow identify this sub-document and then remove it. 
For example, where I match the height field to 160, I'd like to remove the 'baboon' sub-document...
{ Document: {
    monkey: {
        height: 100,
        weight: 40
    },
    baboon: {
        height: 160,
        weight: 70
    }
}

I realise that arrays are another option ($ placeholders and $pull operators), but am trying to avoid as this introduces other complications elsewhere.

A:

You need to make use of the $redact pipeline operator, if you use mongodb v2.6. Unless it is a production environment I strongly recommend you to upgrade your mongodb version to the latest to enjoy the full power of mongodb operators.
This probably is the simplest use case of the $redact stage operator.

Start from the root document, check if the document has an attribute
where height is 160.
If the document has its height equal to 160, PRUNE it. Else
DESCEND on to its sub documents.
If a document at a level does not have an attribute named height,
we assign an empty value to it, so that we can keep that document and
traverse its sub documents.

Code:
db.collection.aggregate([
{$redact:{$cond:
               [{$eq:[{$ifNull:["$height",""]},160]},
                "$$PRUNE",
                "$$DESCEND"
               ]
          }
}
])

o/p:
{
        "_id" : ObjectId("54931b403e20905cb605a802"),
        "monkey" : {
                "height" : 100,
                "weight" : 40
        }
}

You can do an atomic update on the document once you get the document with redacted information:
db.collection.aggregate([
{$redact:{$cond:
               [{$eq:[{$ifNull:["$height",""]},160]},
                "$$PRUNE",
                "$$DESCEND"
               ]
          }
}
]).forEach(function(doc){
   db.collection.update({"_id":doc._id},doc)
})

