2016-12-29 113 views
0

如果我有一個結構查詢數組中貓鼬(節點)

{ 
    "__v": 0, 
    "email": "[email protected]", 
    "password": "dfsdfsdf", 
    "_id": "5864a681c02817571564ddeb", 
    "tokens": [ 
     { 
     "access": "auth", 
     "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.dfsdfsdgsdg3423h4jhj23.7oKGPLMAnSBMYRFTCj-xLwfqIx4q3ZPorM0CNxT3OYA", 
     "_id": "5864a681c02817571564ddec" 
     } 
    ] 
} 

我想令牌(陣列)內的令牌我怎麼做,在JS?

User.find({ 
     [WHAT GOES HERE?] 


    }).then((result) => {return result;}).catch((e) => {return "error"}) 

用戶是我所創建的架構的貓鼬模型。

什麼在查找查詢部分去?

回答

0

如果你有userId這是很容易,或者如果你要查詢一個特定的數據。

userId這裏是userId你在找。

User.find({ id : userId}, {tokens : 1}).then((result) { 
    console.log(result);    // here result will return you the tokens array. 
    console.log(result[0].token); // to fetch the token 
}).catch((e){return "error"}) 
1

使用點符號(。) -

User.find({ 
    'tokens.token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.dfsdfsdgsdg3423h4jhj23.7oKGPLMAnSBMYRFTCj-xLwfqIx4q3ZPorM0CNxT3OYA' 
}).then((result) => {return result;}).catch((e) => {return "error"}) 

而且我猜令牌是用戶唯一的,這樣你可以使用的findOne()代替find()