【问题标题】:How to use populate method of mongoose for mongodb in node js?如何在节点 js 中为 mongodb 使用 mongoose 的填充方法?
【发布时间】:2019-12-05 21:44:43
【问题描述】:

我正在尝试从 mongo 数据库中读取数据,但出现错误。 我会解释我做了什么。

  1. 创建两个架构

    let CompanySchema = new Schema({
        name: {type: String, required: true, max: 100},
        contactPerson: {type: String},
    });
    
    
    // Export the model
    module.exports = mongoose.model('Company', CompanySchema);
    let UserSchema = new Schema({
        name: {type: String, required: true, max: 100},
        companyId:{ type: Schema.Types.ObjectId, ref: 'companyId' }
    });
    // Export the model
    module.exports = mongoose.model('UserTest', UserSchema);
    
  2. 首先,添加一个这样的公司,添加成功

    app.get('/addCompany', async (req, res) => {
    let company = new Company({
        name: 'Test 1',
        contactPerson: 'rajesh'
    })
    company.save(function (err) {
        if (err) {
            console.log(err);
            res.status(500).send(err);
            // return next(err);
        }
        res.send('company added successfully')
        //res.render('index', { title: 'Express'}) 
        });
     })
    

  1. 然后我添加了这样一个用户,添加成功。

    app.get('/addUser', async (req, res) => {
    let user = new User({
        name: 'Test 1',
        companyId: '5d3d46b2825d7f0eaf9d9d27'
    })
    user.save(function (err) {
        if (err) {
            console.log(err);
            res.status(500).send(err);
            // return next(err);
        }
        res.send('user added successfully')
        //res.render('index', { title: 'Express'}) 
       });
    })
    

现在我正在尝试使用 company detail 获取所有用户并出现错误

app.get('/getUser', async (req, res) => {

    User
        .find({})
        .populate('companyId') // only works if we pushed refs to person.eventsAttended
        .exec(function(err, data) {
            if (err) {
             console.log(err)
                return;
            }
            res.send(data);
        });


})

错误

MissingSchemaError: Schema hasn't been registered for model "companyId".
Use mongoose.model(name, schema)
    at new MissingSchemaError (/Users/b0207296/WebstormProjects/untitled2/node_modules/mongoose/lib/error/missingSchema.js:22:11)
    at NativeConnection.Connection.mode

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongodb-query


    【解决方案1】:

    您可以在将第二个架构更改为此后现在尝试吗:

    let UserSchema = new Schema({
        name: {type: String, required: true, max: 100},
        companyId:{ type: Schema.Types.ObjectId, ref: 'Company' }
    });
    
    module.exports = mongoose.model('UserTest', UserSchema);
    

    假设companyId's 在公司的_id 中有匹配的文档。类似的功能也可以通过 mongoDB 的$lookup 来实现。

    【讨论】:

      【解决方案2】:

      在 ref 中,您应该传递公司而不是公司 ID

      let UserSchema = new Schema({
          name: {type: String, required: true, max: 100},
          companyId:{ type: Schema.Types.ObjectId, ref: 'Company' }
      });
      

      【讨论】:

        猜你喜欢
        • 2018-03-28
        • 2018-06-15
        • 2018-10-14
        • 1970-01-01
        • 2019-06-25
        • 2018-12-13
        • 2019-01-14
        • 2013-02-03
        • 2019-06-25
        相关资源
        最近更新 更多