Sim*_*Suh 0 mongodb node.js mongoose-schema
db.bios.find()
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我有这部分:
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();
Run Code Online (Sandbox Code Playgroud)
其中“eBookStore”是我的数据库名称,“books”是我的收藏名称。我知道我在 'db.Book.find()' 中输入 'db' 的地方不正确,但我不知道在引用数据库时那里的代码应该是什么样子。请帮忙!
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
Run Code Online (Sandbox Code Playgroud)
假设您想查看或“阅读”具有特定 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 });
});
});
Run Code Online (Sandbox Code Playgroud)
如果你想得到所有的书:
app.get('/allbooks', (req, res) => {
//find all books
Book.find({}, (err, allBooks) => {
if (err) console.error(err);
res.render('book/list', {allBooks})
})
});
Run Code Online (Sandbox Code Playgroud)
假设您想通过使用带有操作“/daterange”和方法 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});
});
});
Run Code Online (Sandbox Code Playgroud)
假设您想要最新的书:基于相同的原则
Book.findOne().sort({ publicationDate: -1 }).limit(1).exec((err, oneBook) => {
if (err) console.error(err);
//or do wahtever with you like with this oneBook
}
Run Code Online (Sandbox Code Playgroud)
如果你想获得第二个到最后一个
Book.find({}).limit(2).sort({ "_id": -1 }).exec((err, bookSecondLatest) => {
if (err) console.error(err);
// do what ever you want
});
Run Code Online (Sandbox Code Playgroud)
按书名搜索
Book.findOne({bookName: 'Game of thrones'}, (err,oneBook) =>{
console.log(oneBook);
});
Run Code Online (Sandbox Code Playgroud)
小智 5
这是您的架构 1. bookName 2. bookSubtitle 3. PublicationDate 因此,使用其中任何一个,您都可以找到架构详细信息
您已经创建了如下所示的函数,可以从任何地方搜索书籍架构
public getBookDetails(name, callback: CallableFunction) {
Book.find({bookName: name }, (err, book) => {
if (err) {
callback(err);
} else {
callback(book);
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4451 次 |
| 最近记录: |