Leo*_*Joy 0 mongoose mongodb node.js express
注意:我在这里问了这个问题,但当时我纯粹是在工作MongoDB,现在我正在努力实现这一点Mongoose.我认为提出一个单独的问题是合适的,因为我相信答案会有根本的不同,但如果我在该决定中不正确,请告诉我.
我有一个具有以下格式的集合:
[
{
firstname: 'Joe',
lastname: 'Blow',
emails: [
{
email: 'test@example.com',
valid: false
},
{
email: 'test2@example.com',
valid: false
}
],
password: 'abc123',
_id: 57017e173915101e0ad5d94a
},
{
firstname: 'Johnny',
lastname: 'Doe',
emails: [
{
email: 'test3@example.com',
valid: false
}
],
password: 'abc123',
_id: 57017e173915101e0ad5d87b
},
]
Run Code Online (Sandbox Code Playgroud)
我试图找到基于该emails.email字段的用户.这是我到目前为止:
UserModel.find()
.where('emails')
.elemMatch(function (elem) {
elem.where('email').equals(userEmail);
})
.limit(1)
.exec(
(err, usersReturned) => {
console.log('test2@example.com');
});
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我是新手,Mongoose似乎无法解决这个问题.
你可以这样做:
UserModel.find({"emails.email": userEmail}).limit(1).exec(function(err, user){
if(err) console.log("Error: " + JSON.stringify(err));
else if(user) console.log("User Returned is : " + JSON.stringify(user));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
907 次 |
| 最近记录: |