Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何搜索mongoose模式对象?_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何搜索mongoose模式对象?

Node.js 如何搜索mongoose模式对象?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在尝试搜索所有注释,并返回特定用户从我的mongodb创建的所有注释,但当我尝试搜索时,返回的数组为空 我试过: Comment.find({author: {$elemMatch: {username: req.params.username}}}, (err, foundComments) => { // Code goes here }); Mongoose注释模式: const mongoose = require('mongoose'); co

我正在尝试搜索所有注释,并返回特定用户从我的mongodb创建的所有注释,但当我尝试搜索时,返回的数组为空

我试过:

Comment.find({author: {$elemMatch: {username: req.params.username}}}, (err, foundComments) => {
        // Code goes here
      });
Mongoose注释模式:

const mongoose = require('mongoose');

const commentSchema = new mongoose.Schema({
  text: String,
  discussion:[{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Comment',
  }],
  createdAt: {
    type: Date,
    default: Date.now,
  },
  author: {
    id: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    },
    username: 'String',
    avatar: {
      image: String,
      imageId: String,
    },
  },
  likes: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
  }],
  dislikes: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
  }],
});

module.exports = mongoose.model('Comment', commentSchema);
当我运行它时,我希望
foundComments
是一个数组
[array of comments]
,但我只得到一个空数组
[]
Comment.find({“author.username”:req.params.username})注释.find({“author.username”:req.params.username})

Comment.find({"username": req.params.username},{"discussion":1,"_id":0})