猫鼬不返回数据进行第二次收集

时间:2018-09-17 04:13:34

标签: mongodb mongoose

我有一个无服务器应用程序,该应用程序不为我的一个集合返回任何值。

我的函数向Mongo查询3个集合。

即使locationTypes收集包含5个文档,也不会返回任何文档。位置和失败集合会返回数据。

const Location = require('./models/Location');
const LocationType = require('./models/LocationType');
var locationPromise = Location.find();
var locationTypePromise = LocationType.find();
var failurePromise = Failure.find();

Promise.all([locationPromise, locationTypePromise, failurePromise])
    .then(values => res.json({ locations: values[0], locationTypes: values[1], failures: values[2] }))

models / Location.js

const mongoose = require('mongoose');
const LocationSchema = new mongoose.Schema({
  Description: String,
  Type: String,
  Parent: String
});

module.exports = mongoose.model('Location', LocationSchema);

models / LocationType.js

const mongoose = require('mongoose');
const LocationTypeSchema = new mongoose.Schema({
  Description: String
});

module.exports = mongoose.model('LocationType', LocationTypeSchema);

问题可能与具有复合名称的集合有关吗?

1 个答案:

答案 0 :(得分:1)

此问题来自猫鼬如何解释模型名称。为Mongoose提供模型名称时,它将自动将其复数以找到相关的MongoDB集合名称。诸如location之类的简单单词很容易复数为locations,但是诸如此类的复合名称诸如locationType不能复数为locationTypes

const LocationTypeSchema = new mongoose.Schema({ Description: String }, { collection: "locationTypes" });

添加{ collection: "locationTypes" }告诉Mongoose LocationType模式应映射到locationTypes集合。

请参阅猫鼬的official documentation on this