好吧,这对我有用:
集合:“库存”
const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://localhost/testing";
router.get('/', function(req, res) {
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
if (err) throw err;
let dbo = db.db("testing");
var stream = dbo.collection("inventory").find().stream()
stream.on('error', function (err) {
console.error(err)
})
stream.on('data', function (doc) {
console.log(doc)
})
.on('end', function(){
// final callback
console.log("iteration complete")
});
});
//post blank page
res.writeHead(200, {'Content-Type': 'application/json'});
res.end();
});