0

I have imported a csv file containing Programme data into a MongoDB collection. One of the fields, tags, is empty for some of the documents. Example:

"_id" : ObjectId("5906252136ac939ea93b4110"),
"pid" : "b00mtw7l",
"epoch_start" : ISODate("2009-02-05T08:16:00Z"),
"epoch_end" : ISODate("2009-02-12T08:16:00Z"),
"complete_title" : [
    "big and small",
    " series 1",
    " theres space for small"
],
"media_type" : "video",
"tags" : [""] //empty field, how to delete?

I want to delete the the empty tag fields entirely to reduce the redundancy of the data. How do I do this through the mongo shell rather than altering the csv file before importing it?

Thanks.

1
  • Do all of them have empty tags like this? Or some have actual tags? Commented May 11, 2017 at 18:44

1 Answer 1

3

Do it with

db.collection.update({tags: [""]},{$unset: {tags:1}},{multi: true});

{multi: true} will remove tags from multiple documents, wherever applicable.

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

2 Comments

Doesn't this remove the entire document? I want to just remove the tags field when it is empty.
@sums22 sorry didn't read the question properly. Updated my answer.

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.