Pri*_*tra 0 collections mongoose mongodb
我是 node 和 mongodb 的新手。我正在尝试从另一个模型(公司)查询不同的模型(事件)。
基本上在Event模型中有一个名为company. 我想获得 id 是事件 ID 的公司。
我有一个数组中的所有事件 ID。
let eventIds = [ 5b76a8139dc71a4a12564cd2,
5b9a1685c239342d4635466c,
5b8e753bdbccf803e906aaeb ]
Run Code Online (Sandbox Code Playgroud)
事件架构——
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}
]
});
Run Code Online (Sandbox Code Playgroud)
在公司模式中——
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);
});
});
Run Code Online (Sandbox Code Playgroud)
我收到Event.find is not a function错误消息。如何从另一个模型(公司)查询不同的模型(事件)
任何帮助都受到高度赞赏。
不知道为什么,但我必须通过以下方式做到这一点。
Event.find({ $in: eventIds}, function(err, docs){
Run Code Online (Sandbox Code Playgroud)
到
mongoose.model('Event').find({_id:eventIds}, function(err, docs){
Run Code Online (Sandbox Code Playgroud)
它返回了 3 个正确的文件。