mongodb - 如何删除具有特定值

时间:2017-04-06 15:30:52

标签: javascript mongodb

我从http://www.omdbapi.com/下载了大量数据,并将数据存储在MongoDB中。

问题是,我有很多字段"N/A",如何删除所有这些字段,不知道字段名称。

例如,从本文档中。

{
  "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"
}  

我想收到这份文件。

{
  "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"
}

1 个答案:

答案 0 :(得分:0)

您可以迭代对象并检查值。

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]];
}