考虑以下帖子集合:
{
_id: 1,
title: "Title1",
category: "Category1",
comments: [
{
title: "CommentTitle1",
likes: 3
},
{
title: "CommentTitle2",
likes: 4
}
]
}
{
_id: 2,
title: "Title2",
category: "Category2",
comments: [
{
title: "CommentTitle3",
likes: 1
},
{
title: "CommentTitle4",
likes: 4
}
]
}
{
_id: 3,
title: "Title3",
category: "Category2",
comments: [
{
title: "CommentTitle5",
likes: 1
},
{
title: "CommentTitle6",
likes: 3
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想检索所有帖子,如果一篇帖子有 4 个赞的评论,我只想在“comments”数组下检索此评论。如果我这样做:
db.posts.find({}, {comments: { $elemMatch: {likes: 4}}})
Run Code Online (Sandbox Code Playgroud)
...我明白了(这正是我想要的): …
我正在通过POST方法验证表单.我想在已注册电子邮件地址时将错误输入express-validator,因此当我执行req.validationErrors()时,它会被包括在内.像这样的东西:
req.checkBody('inputFirstName', 'First name empty').notEmpty();
req.checkBody('inputLastName', 'Last name empty').notEmpty();
db.User.find({email: req.body.inputEmail}, function (err, user){
if(user){
var error = {param: "inputEmail", msg: "Email address already registered", value: ''};
expressValidator._errors.push(error);
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢.