Node.js 如何使用Mongoose从MongoDB读取数据?
在MongoDB文档中,它表示要执行以下操作:Node.js 如何使用Mongoose从MongoDB读取数据?,node.js,mongodb,mongoose-schema,Node.js,Mongodb,Mongoose Schema,在MongoDB文档中,它表示要执行以下操作: db.bios.find() 在我的代码中,我有以下部分: mongoose.connect('mongodb://localhost:27017/eBookStore'); let newBookSchema = new mongoose.Schema({ bookName: {type: String}, bookSubtitle: {type: String}, publicationDate: {type: Number,
db.bios.find()
在我的代码中,我有以下部分:
mongoose.connect('mongodb://localhost:27017/eBookStore');
let newBookSchema = new mongoose.Schema({
bookName: {type: String},
bookSubtitle: {type: String},
publicationDate: {type: Number, default: new Date().getTime()}
});
let Book = mongoose.model('books', newBookSchema);
db.Book.find();
其中“eBookStore”是我数据库的名称,“books”是我收藏的名称。我知道我在“db.Book.find”中键入“db”的位置不正确,但我不知道引用数据库时那里的代码应该是什么样子。请帮忙 您可以参考猫鼬的文档 使用下面的链接转到特定模型 前 let Book=mongoose.模型“Book”,newBookSchema,“books” //第一个参数是mongoose模型名,第二个模式和mongodb中集合的第三个名称
Book.find{},functionerr,arr{} 使用相同的代码,一旦您通过mongoose连接mongo,就可以直接进行搜索,无需提及db。如果您有要在其中搜索的集合的架构,则此选项始终有效
mongoose.connect('mongodb://localhost:27017/eBookStore');
let newBookSchema = new mongoose.Schema({
bookName: {type: String},
bookSubtitle: {type: String},
publicationDate: {type: Number, default: new Date().getTime()}
});
let Book = mongoose.model('book', newBookSchema);
Book.find({queryForFilters});
请注意,我删除了代码第9行书本中的“s”。这是因为mongoose以复数和小写形式保留第一个参数,因此将book作为第一个参数传递将产生一个名为books的集合
mongoose.connect('mongodb://localhost:27017/eBookStore',{useNewUrlParser:true});
//Schema model
let newBookSchema = new mongoose.Schema({
bookName: {type: String},
bookSubtitle: {type: String},
publicationDate: {type: Number, default: new Date().getTime()} // i will have used new Date() only for future data query based on date
});
let Book = mongoose.model('Book', newBookSchema); // Capital letter will be better for distinguish from a normal variable and to remember easly
假设您想查看或阅读具有特定id的特定书籍,您可以创建路由器并读取数据
app.get('/view/:id', (req, res) => {
var bookId = req.params.id;
//read the data part
Book.findOne({ _id: bookId }, (err, oneBook) => {
if (err) console.error(err);
//pass data to the template engine
res.render('book/sale', { oneBook });
});
// Or you can use findById()
Book.findById({ bookId }, (err, oneBook) => {
if (err) console.error(err);
//pass data to the template engine
res.render('book/sale', { oneBook });
});
});
如果你想得到所有的书:
app.get('/allbooks', (req, res) => {
//find all books
Book.find({}, (err, allBooks) => {
if (err) console.error(err);
res.render('book/list', {allBooks})
})
});
假设您希望通过使用带有action/daterange和method POST的表单从html模板中选择两个日期之间的书籍
app.post('/daterange', (req, res) => {
//date input from template engine named start
var startDate = new Date(req.body.start);
//date input from template engine named end
var endDate = new Date(req.body.end);
Book.find({ "publicationDate": { "$gte": startDate, "$lte": endDate } }, (err, booksByDate) => {
var startDate = new Date(req.body.start); // this is why you should use new Date() only for query simplicity
var endDate = new Date(req.body.end);
if (err) console.error(err);
res.render('book/bookbydate', { booksByDate});
});
});
假设你想要一本最新的书:基于同样的原则
Book.findOne().sort({ publicationDate: -1 }).limit(1).exec((err, oneBook) => {
if (err) console.error(err);
//or do wahtever with you like with this oneBook
}
如果你想得到倒数第二名
Book.find({}).limit(2).sort({ "_id": -1 }).exec((err, bookSecondLatest) => {
if (err) console.error(err);
// do what ever you want
});
按书名搜索
Book.findOne({bookName: 'Game of thrones'}, (err,oneBook) =>{
console.log(oneBook);
});
这是您的模式
1.书名
2.书名
3.出版日期
因此,使用任何一种模式,您都可以找到模式的详细信息
您已经创建了如下函数,可以从任何地方搜索图书模式
我有一个使用nodejs和mongoose的公共应用程序。这对你有帮助吗
public getBookDetails(name, callback: CallableFunction) {
Book.find({bookName: name }, (err, book) => {
if (err) {
callback(err);
} else {
callback(book);
}
});
}