为什么我的查询在mongo shell中显示数据而不在node中显示数据?

时间:2019-05-31 19:45:18

标签: node.js mongodb mongoose

我正在研究一个模块,以手动导入在数据库升级期间丢失的数据。我创建了JSON,然后对名为loaddata的集合进行了mongoimport。

在mongo shell中,运行db.loaddata.find()会返回所有18个订单,就像它们应该的一样。

当我尝试从节点运行它时,我得到0个订单(没有错误),但是我不明白为什么它不返回结果,除了我可能搞砸了数据库模型之外?这是我的代码:

module.exports.lostOrdersGetAll = function(req, res) {
  console.log('GET the lost orders');
  LostData.find({}).exec(function(err, orders) {
    if (err) {
      console.log('Error finding lost orders');
      res.status(500).json(err);
    } else {
      console.log('Found lost orders', orders.length);
      res.json(orders);
    }
  });
};

我的模特

var mongoose = require('mongoose');

var lostdataSchema = new mongoose.Schema({
  cart: {
    username: String,
    items: [],
    totalQty: Number,
    totalPrice: Number
  },
  customer: String,
  paymentID: {
    type: String,
    required: true
  },
  paymentNote: String,
  refundedAmt: Number,
  paymentStatus: {
    type: String
  },
  user: String,
  createdOn: {
    type: Date,
    default: Date.now
  },
  updatedOn: {
    type: Date
  },
  active: Boolean
});

mongoose.model('LostData', lostdataSchema);

我的db.js文件,尽管我怀疑问题出在此文件中:

var mongoose = require('mongoose');
var options = {
  useMongoClient: true
};
const dburl = 'mongodb://localhost:27017/rumble';

var retry = null;

mongoose.connect(dburl, options);

//CONNECTION EVENTS
mongoose.connection.on('connected', function() {
  console.log('Mongoose connected to', dburl);
});

mongoose.connection.on('disconnected', function() {
  console.log('Mongoose disconnected', dburl);
});

mongoose.connection.on('error', function(err) {
  console.log('Mongoose connection error', err);
});
//CAPTURE APP TERMINATION / RESTART EVENTS
//To be called when process is restarted or terminated
function gracefulShutdown(msg, callback) {
  mongoose.connection.close(function() {
    console.log('Mongoose disconnected through ' + msg);
    callback();
  });
}

//For nodemon restarts
process.once('SIGUSR2', function() {
  gracefulShutdown('nodemon restart', function() {
    process.kill(process.pid, 'SIGUSR2');
  });
});
//For app termination
process.on('SIGINT', function() {
  gracefulShutdown('App termination (SIGINT)', function() {
    process.exit(0);
  });
});
//For Heroku app termination
process.on('SIGTERM', function() {
  gracefulShutdown('App termination (SIGTERM)', function() {
    process.exit(0);
  });
});

//bring in schemas and models
require('./admins.model.js');
require('./articles.model.js');
require('./orders.model.js');
require('./lostdata.model.js');
require('./fields.model.js');
require('./pages.model.js');
require('./alerts.model.js');
require('./products.model.js');
require('./registrations.model.js');
require('./users.model.js');
require('./volunteers.model.js');
require('./winners.model.js');
require('./stripepayments.model.js');
require('./stripecharges.model.js');
require('./cartstore.model');
require('./waiverrecipients.model');

从控制台记录的结果(对我来说,这表明代码正在正确运行,并且未返回错误): Console logs for running module.exports.lostOrdersGetAll 我的Mongo Shell结果: 18 records found, as expected

1 个答案:

答案 0 :(得分:2)

用于mongoose.model()方法

  

第一个参数是您的模型所针对的集合的单数名称。猫鼬会自动查找模型名称的复数形式,小写形式。因此,对于上面的示例,“坦克”模型用于数据库中的坦克集合。

猫鼬试图变得聪明并检测模型名称,但是您可以像这样强制其使用您想要的集合

new Schema({..}, { collection: 'lostdata' })

或者在创建模型时,就像这样

mongoose.model('LostData', lostdataSchema, 'lostdata')