Frage

So I've got an application that uses a lot of embedded documents, which is fine. However I've noticed that some embedded documents aren't displayed when you show collections in Mongo shell.

Normally this isn't an issue, but whilst dicking with setting embedded documents, I accidentally added an empty entry to one of the entries. I'd normally do something like this to remove the entry db.collection.remove({_id: ObjectId('<OBJECT_ID>')}), but since some of these aren't actual collections I'm unable to do it like this.

I'm also not sure how I'd splice out this successfully while actually removing the document. I could splice it out of the entry, but I have a feeling it would leave that embedded document floating around somewhere in the DB.

Any ideas how to do this?

To give you an idea what I'm talking about, an example of the entry:

entry = {
    _id: ObjectId('blah blah blah'),
    name: {
        first: 'example',
        last: 'city'
    },
    log : [
        {
            _id: ObjectId('some id'),
            action: 'whatever',
            someField: 'etc.'
        },
        {
            _id: ObjectId('another id')
        },
        {
            _id: ObjectId('yet another id'),
            action: 'who cares',
            someField: 'data'
        }
    ]
}
War es hilfreich?

Lösung

If you want to remove one of the log entries, then you want the $pull operator.

The format would be something like:

db.collection.update({_id:<id-of-document-to-update>},
                     {$pull:{"log._id":<id-of-log-entry-to-remove>"}}
)

This says, find document with certain _id and remove from log array an entry with certain sub_id.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top