我在用户数组中有friends
数组,我需要通过电子邮件获取特定用户的所有friends
,approved
字段为true
在nodejs中,我有用户mongo模式
const UserSchema = mongoose.Schema({
username : {type: String , required :true},
email : {type: String , required :true, unique: true, match: /.+\@.+\..+/},
password : {type: String , required :true},
friends : {type:[{username : String, approved: Boolean}]},
});
[
{
"username": "ali",
"email": "ali200@gmail,com",
"password": "pdcjjfefmkadjakefkeofjafjafsfjsalnnryifajs",
"friends": [
{
"id": "1",
"username": "gamal",
"approved": true
},
{
"id": "2",
"username": "osama",
"approved": false
},
{
"id": "3",
"username": "john",
"approved": true
}
]
}
]
我需要得到friends
对象的数组,approved
字段是true
,如下所示
[
{
"id": "1",
"username": "gamal",
"approved": true
},
{
"id": "3",
"username": "john",
"approved": true
}
]
注解中的代码:
router.get("/getFriends", (req, res) => {
var email = getEmailFromToken(req.header("Authorization"));
User.findOne({
email: email
}).then((user) => res.status(200).send({
friends: user.friends
}));
1条答案
按热度按时间li9yvcax1#
通过使用代码,可以使用
也可以使用查询
Mongo测试仪:mongo-playground