Model.find不是猫鼬中的函数

编程入门 行业动态 更新时间:2024-10-21 11:51:51
本文介绍了Model.find不是猫鼬中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是node和mongodb的新手.我正在尝试从另一个模型(公司)查询另一个模型(事件).

I am new in node and mongodb. I am trying to query a different model(Event) from another model(Company).

基本上,在 Event 模型中,有一个名为 company 的字段.我想获得ID为事件ID的公司.

Basically in Event model there is a field called company. I would like to get the company where id is an Event ID.

我将所有事件ID放在一个数组中.

I have all the event IDs in an array.

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不是函数错误消息.如何从另一个模型(公司)查询另一个模型(事件)

I am getting Event.find is not a function error message. How can I query a different model(event) from another model(company)

我们非常感谢您的帮助.

Any help is highly appreciated.

推荐答案

不知道为什么,但是我必须按照以下方式进行.

Not sure why but I had to do this in the following way.

Event.find({ $in: eventIds}, function(err, docs){

收件人

mongoose.model('Event').find({_id:eventIds}, function(err, docs){

返回了3个正确的文档.

which returned 3 documents which are correct.

更多推荐

Model.find不是猫鼬中的函数

本文发布于:2023-10-30 04:17:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Model   find   猫鼬中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!