Как определить схему для документа Mongodb с неструктурированными массивами? - PullRequest
0 голосов
/ 05 апреля 2019

Я пытаюсь создать сайт nodejs для отображения содержимого данных, которые я собрал и сохранил в коллекции mongodb. Я могу выполнить очистку и получить данные в БД, но не могу их вернуть. Я предполагаю, что проблема не в правильном определении схемы в моей модели узла.

Данные - это массивы строк.

{
    "_id": {
        "$oid": "5ca553dc97c32c1f05aba38d"
    },
    "title": [
        "Official Foreign Reserves(March 2019)"
    ],
    "date": [
        "2019.04.03"
    ],
   "summary": [
        "The Consumer Price Index was 104.49(2015=100) in March 2019. The index decreased 0.2 percent from the preceding",
        "month and rose 0.4 percent from the same month of the previous year.",
        "The index excluding food and energy was 105.32 in March 2019. The index decreased 0.1 percent from the preceding",
        "month and rose 0.8 percent from the same month of the previous year.",
        "For more information, refer to the attached file.",
        ""
    ]
}

Я пытался определить схему следующим образом:

const ScrapeSchema = new Schema({
  title:{
    type: Array, "default" : []
  },
  date:{
    type: Array, "default" : []
  },
  summary:{
    type: Array, "default" : []
  }
});

Объект, который я возвращаю, пуст. Mongodb подключается нормально и ошибок нет.

В настоящее время я просто использую find ({}) в запросе.

Ответы [ 2 ]

0 голосов
/ 05 апреля 2019

Попробуйте это:

let ScrapeSchema = new Schema({      
    title: { type: [String], default: [] },
    date: { type: [String], default: [] },
    summary: { type:[String], default: [] }
});

или date: { type: [Date] }.

0 голосов
/ 05 апреля 2019

Если я вас правильно понимаю,

let ScrapeSchema = new Schema({      
        title: {
            type: [String],
            required: true
          },
          date: {
            type: [String],
            required: true
          },
          summary: {
                type:[String]
            }
    });

После экспорта модели вы можете найти другой модуль с Scarape.model.find, или как хотите. Кстати, вы можете использовать тип даты, как date: { type: Date, default: Date.now },

...