我试图搜索所有注释并从mongodb中返回特定用户创建的所有注释,但是当我尝试搜索返回的数组时却是空的。
我尝试过:
button.addEventListener('onclick', event => {
...;
});
猫鼬评论模式:
Comment.find({author: {$elemMatch: {username: req.params.username}}}, (err, foundComments) => {
// Code goes here
});
运行它时,我期望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 ]
。
答案 0 :(得分:0)
Comment.find({"username": req.params.username},{"discussion":1,"_id":0})
答案 1 :(得分:0)
Comment.find({"author.username":req.params.username});