本文介绍了在猫鼬中查找一个子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 Mongoose 中对子文档尝试 findOne 查询,但我运气不佳...
I am attempting a findOne query in Mongoose on a subdocument but I'm not having much luck...
我的架构如下所示:
var Team = mongoose.Schema({
teamName: String,
teamURL: String,
teamMembers: [{username: String, password: String, email: String, dateCreated: Date}],
});
var Team = db.model('Team', Team);
我只需要从我使用此查询的文档中找到用户的电子邮件
I need to simply find the users email from the document in which I am using this query
Team.findOne({'teamMembers.username': 'Bioshox'}, {'teamMembers.$': 1}, function (err, team) {
if (team) {
console.log(team[1].email);
}
});
任何帮助将不胜感激!
推荐答案
您缺少对象的 teamMembers
级别,因此您的代码需要更改为如下所示:
You're missing the teamMembers
level of your object, so your code needs to change to something like this:
Team.findOne({'teamMembers.username': 'Bioshox'}, {'teamMembers.$': 1},
function (err, team) {
if (team) {
console.log(team.teamMembers[0].email);
}
}
);
这篇关于在猫鼬中查找一个子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!