【发布时间】:2019-02-27 22:20:07
【问题描述】:
我是 node 和 mongodb 的新手。我正在尝试从另一个模型(公司)查询不同的模型(事件)。
基本上在Event 模型中有一个名为company 的字段。我想获取 id 是 Event ID 的公司。
我有一个数组中的所有事件 ID。
let eventIds = [ 5b76a8139dc71a4a12564cd2,
5b9a1685c239342d4635466c,
5b8e753bdbccf803e906aaeb ]
事件架构 --
var EventSchema = new Schema({
title:{type:String,require:true,index:true},
description:{type:String,require:false},
companies:[
{type:Schema.Types.ObjectId,ref:"Company",require:true,index:true}
]
});
在公司模式中--
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
Event = require('./event.js');
var CompanySchema = new Schema({
name:{type:String,require:true,index:true},
description:{type:String,require:false}},{
//no auto indexing at the beginning
autoIndex:true,
//no strict to save changes in the valuesBeforeChange field.
strict:false}
);
CompanySchema.static("searchCompanies",function(callback,criteria){
"use strict";
var That = this;
var query = That.find();
async.waterfall([
function(callback){
let eventIds = [5b76a8139dc71a4a12564cd2,5b9a1685c239342d4635466c,5b8e753bdbccf803e906aaeb ];
Event.find({ $in: eventIds}, function(err, docs){
console.log(docs);
});
}
],function(err,companyResultObj){
callback(err,companyResultObj);
});
});
我收到Event.find is not a function 错误消息。如何从另一个模型(公司)查询不同的模型(事件)
非常感谢任何帮助。
【问题讨论】:
-
你能发布事件架构吗
-
@AnthonyWinzlet 当然。
-
@AnthonyWinzlet 我已经用事件模式更新了帖子。
-
您缺少导出模型。添加此行
export default Mongoose.model('Event', EventSchema)或module.exports = Mongoose.model('Event', EventSchema) -
@AnthonyWinzlet 现在得到
EventSchema is not defined未定义。
标签: mongodb mongoose collections