Getting and using the "Ou id" in mongodb

Keywords: Database MongoDB git


 

1, query parameters in git form

For git path parameters, click a link to get / lydetail?id in the URL bar, and then you can receive this id in the index.js file. The route name is / lydetail

<a href="/lydetail?id=<%-item._id %>"><%-item.title %></a>

<table class="table">
  		<tr><th>Serial number</th><th>Title</th><th>content</th></tr>
  		<% list.map(function(item,i){ %>
  			<tr>
  				<td><%-i+1 %></td>

                       <!--click a Links can be found in the URL bar/lydetail?id-->
  				<td><a href="/lydetail?id=<%-item._id %>"><%-item.title %></a></td>
                
  				<td><%-item.con %></td>
  			</tr>
  		<% }) %>
 </table>

 

2. Receive / lydetail?id in index.js

Note: at this time, the ID obtained by req.query.id is not the "Ou ID" in mongodb, so you must obtain it with the following object

var ObjectId = require('mongodb').ObjectId;

id = ObjectId(req.query.id); at this time, the id is the id passed from ajax, which can be compared with the database

var ObjectId = require('mongodb').ObjectId;   //mongodb's own object


router.get('/lydetail',(req,res)=>{
	console.log(req.query)   //get parameters
	var id = ObjectId(req.query.id);   //At this time, the id is the id passed from ajax, which can be compared with the database

	mongodb.connect(db_str,(err,database)=>{
		database.collection('goods',(err,coll)=>{
            coll.find({_id:id}).toArray(function(err,data){
                 res.render('liuyan',{list:data[0].con})  //The list value can be rendered to the liuyan.ejs page
				 database.close()
            })




			//coll.find({}).sort({_id:-1}).toArray((err,data)=>{
			//	res.render('liuyan',{list:data})
			//	database.close()
			//})
		})

	})
	
	
})

Then click the a tab to get the id corresponding to the database, and then you can get the value corresponding to the database, and then you can render the page (which can be used to click to enter the details page, for example: news list)

 

...... Then the page displays

Posted by JovanLo on Tue, 31 Dec 2019 23:26:09 -0800