【问题标题】:find documents through Model in mongoose在mongoose中通过Model查找文档
【发布时间】:2021-09-01 22:03:18
【问题描述】:

我正在学习猫鼬并决定运行这段代码

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test');

const personSchema = new mongoose.Schema({
    name: String,
    age: Number,
});

const Person = mongoose.model('Person', personSchema);

const person = new Person({
    name: 'John',
    age: 25,
});

person.save().then(()=> console.log('inserted'));

Person.find( function (err, docs) {
    if(err){
        console.log(err);
    }else{
        console.log(docs);
    }
});

但是它似乎没有像我预期的那样工作(我想打印与模型 Person 相关的集合的每个文档)。相反,我收到了这个警告/错误:

(node:645135) UnhandledPromiseRejectionWarning: MongoInvalidArgumentError: Method "collection.find()" 在 Collection.find 中最多接受两个参数 ...

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    两个参数。第一个是条件,第二个是函数。

    Person.find({ }, function (err, docs) { 
        if(err){
            console.log(err);
        }else{
            console.log(docs);
        }
    }); 
    

    【讨论】:

      猜你喜欢
      • 2013-02-06
      • 2021-03-23
      • 1970-01-01
      • 2016-03-24
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      相关资源
      最近更新 更多