V
V
Vlad Demchuk2021-01-12 11:58:23
Node.js
Vlad Demchuk, 2021-01-12 11:58:23

How to use Mongo DB Atlas to display data on an HTML page using Node JS?

Let's say I have objects in the database with the following structure:
5ffd632364d58990129828.png

How can I use the database to display information on an HTML page (so that I can access each element of the object separately).

Does anyone know a good video?
I figured out how to create an application for writing to the database, but now I don’t know how to extract data))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arkady Baganin, 2021-01-12
@VladDemchuk

Hey! I would suggest reading the mongoose documentation . Let me show you how I did it :)

Connecting

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true, useUnifiedTopology: true});


You create a schema (it looks like a class from js)

const carsScheme = new mongoose.Schema({ // описываем схему
  title: String, // задаем перечень свойств (Название: тип)
  money: Numerable,
  descriptions: String,
  isVIP: Boolean
});

const cars = mongoose.model('Cars', carsScheme); // Создаем модель по схеме в бд ('Название модели в бд', схема)


We create and receive

var carA = {
  title: "Volva",
  money: 1000,
  descriptions: "...",
  isVIP: true
};

var carB = {
  title: "Mustang",
  money: 3700,
  descriptions: "...",
  isVIP: false
};

cars.Create([carA, carB]); // Добавляем 2 новых машины
cars.Save(); // Сохраняем

/*
* Получаем тачки
*/

var carVIPS = Tank.find({ isVIP: true }); // Делаем запрос к машинам со статусом "вип"
console.log(carVIPS); // Смотрим результаты

mongoose.close(); // закрываем бд


PS I didn't test the code, but you got the point :) Learn the mat. part

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question