How to use mongoose to read an existing database

in mongoose, you need to define schema, to insert a database. Here is a problem: how do I read a value that is not a table I created? here is the code

related codes

/ / Please paste the code text below (do not replace the code with pictures)

   

what result do you expect? What is the error message actually seen?

found that the output content content is the same, all baidus this table data, ask why this is it!

Jun.22,2021

you need to refer to the field types in the (document) of the document to be read. For example, I want to read the users document. The format of the document is as follows:

here is the reference code:

var mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/demodb')

var db = mongoose.connection;
db.on('error', function(error) {
  console.log(error);
});

// schema
var users = new mongoose.Schema({
  name: {type : String, default : 'foo'},
  password: String
});
mongoose.model('User', users);

// 
var User = mongoose.model('User');
User.find((err, users) => {
  console.log(users)
  db.close()
});
Menu