简体   繁体   中英

mongodb — how to delete fields that have certain value

I downloaded a lot of data from http://www.omdbapi.com/ and I stored the data in MongoDB.

The problem is, that I got a lot of fields with "N/A" , how can I delete all those fields, without knowing the fields names.

For example, from this document.

{
  "Title": "The Top 14 Perform",
  "Year": "2008",
  "Rated": "N/A",
  "Released": "02 Jul 2008",
  "Season": "4",
  "Episode": "12",
  "Runtime": "60 min",
  "Genre": "Game-Show, Music, Reality-TV",
  "Director": "Don Weiner",
  "Writer": "Simon Fuller (creator), Nigel Lythgoe (creator)",
  "Actors": "Joshua Allen, Stephen Boss, Cat Deeley, Matthew Dorame",
  "Plot": "Host Cat Deeley promised at the outset that the final 14 dancers will face some changes and the competition would get more difficult for the final seven couples...",
  "Language": "N/A",
  "Country": "N/A",
  "Awards": "N/A",
  "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BMTMxMjU0MTMxMl5BMl5BanBnXkFtZTcwNjY4Mjc3MQ@@._V1_SX300.jpg",
  "Metascore": "N/A",
  "imdbRating": "5.3",
  "imdbVotes": "13",
  "imdbID": "tt1234567",
  "seriesID": "tt0472023",
  "Type": "episode",
  "Response": "True"
}  

I want to get this document.

{
  "Title": "The Top 14 Perform",
  "Year": "2008",
  "Released": "02 Jul 2008",
  "Season": "4",
  "Episode": "12",
  "Runtime": "60 min",
  "Genre": "Game-Show, Music, Reality-TV",
  "Director": "Don Weiner",
  "Writer": "Simon Fuller (creator), Nigel Lythgoe (creator)",
  "Actors": "Joshua Allen, Stephen Boss, Cat Deeley, Matthew Dorame",
  "Plot": "Host Cat Deeley promised at the outset that the final 14 dancers will face some changes and the competition would get more difficult for the final seven couples...",
  "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BMTMxMjU0MTMxMl5BMl5BanBnXkFtZTcwNjY4Mjc3MQ@@._V1_SX300.jpg",
  "imdbRating": "5.3",
  "imdbVotes": "13",
  "imdbID": "tt1234567",
  "seriesID": "tt0472023",
  "Type": "episode",
  "Response": "True"
}

You can iterate on the object and check for the value.

var o = {
  "Title": "The Top 14 Perform",
  "Year": "2008",
  "Rated": "N/A",
  "Released": "02 Jul 2008",
  "Season": "4",
  "Episode": "12",
  "Runtime": "60 min",
  "Genre": "Game-Show, Music, Reality-TV",
  "Director": "Don Weiner",
  "Writer": "Simon Fuller (creator), Nigel Lythgoe (creator)",
  "Actors": "Joshua Allen, Stephen Boss, Cat Deeley, Matthew Dorame",
  "Plot": "Host Cat Deeley promised at the outset that the final 14 dancers will face some changes and the competition would get more difficult for the final seven couples...",
  "Language": "N/A",
  "Country": "N/A",
  "Awards": "N/A",
  "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BMTMxMjU0MTMxMl5BMl5BanBnXkFtZTcwNjY4Mjc3MQ@@._V1_SX300.jpg",
  "Metascore": "N/A",
  "imdbRating": "5.3",
  "imdbVotes": "13",
  "imdbID": "tt1234567",
  "seriesID": "tt0472023",
  "Type": "episode",
  "Response": "True"
} 

var toDelete = [];
for each (var prop in o) {
    if (o[prop] == "N/A") toDelete.push(prop);
}
for (var i=0; i < toDelete.length; i++) {
    delete o[toDelete[i]];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM