Node.js 如何搜索mongoose模式对象?
我正在尝试搜索所有注释,并返回特定用户从我的mongodb创建的所有注释,但当我尝试搜索时,返回的数组为空 我试过: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
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})