2017-07-15 118 views
-2

對象價值物是這樣的:如何從貓鼬

{ 
      "_id" : ObjectId("5969b38a9ab0c60370a6dea3"), 
      "name" : "789", 
      "dec" : "789", 
      "regular" : "789", 
      "sale" : "78", 
      "stock" : "8", 
      "cat" : { 
        "wallets" : [{ 
            "selected" : "true", 
            "subcategory" : "leather" 
          }] 
      }, 
      "imgs" : [ 
        "file-1500099465305.png", 
        "file-1500099465310.png" 
      ], 
      "__v" : 0 
    } 

我的問題是

如何在MongoDB中通過查詢檢索特定值?

+0

歡迎來到Stack Overflow。請花一些時間閱讀https://stackoverflow.com/help/how-to-ask –

回答

0

通過查詢導入模型,找到您的實例:

export function myFunc(req, res) { 
      return Model.find({ 'name.last': 'Ghost' }} //your condition 
      }) 
      .then(entity=> { 
       if (entity) { 
       console.log(entity)     // to see object 
       res.status(200).json({'entity': entity}).end(); 
       } else { 
       return res.status(404).end(); 
       } 
      }).catch(handleError(res));    // error handling 
     } 

現在,當你調用這個函數,你會得到實體對象,你可以從它那裏得到實體對象鍵。