How to get documents with Mongoose?

Viewed 110

i start with mongoDB and mongoose (i'm a beginner) When i use the following code, the console return me an empty array "[]" instead of my 4 documents in my mongoDB DB.

On mongoDb atlas, i have a DB "Dyma", in "Dyma" i have a collection "Chapter", and in chapter i have 4 documents. The link "mongodb://alex:qwe@localhost:27017/dyma" is correct and the user too because if i modify it, i have an error.

Can someone tell me what is the problem ?

Thanks a lot

const schema = mongoose.Schema;

const chapterSchema = schema({
  title: String,
  nbOfLessons: Number,
  index: Number,
  active: Boolean,
});

const Chapters = mongoose.model("chapters", chapterSchema);

mongoose
  .connect("mongodb://alex:qwe@localhost:27017/dyma", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
  .then(() => {
  console.log("Connection OK !");
  Chapters.find({}, (err, documents) => {
    if (err) {
      console.log(err);
    }
    console.log(documents);
  });
})
  .catch((err) => {
  console.log(err);
  });
0 Answers
Related