2014-02-14 43 views
0

找到的文件我有如下結構的用戶模式:的Node.js中的一個元素數組

new Schema({ 
      email : String, 
      password : String, 
      shoppingCart : [{type : Schema.Types.ObjectId, ref : 'Product'}] 
     }); 

,我也有一個產品架構如下:

new Schema({ 
      title : String, 
      description : String, 
      vendorId : String, 
      stock : Number 
     }); 

我怎麼能搜索在購物車內有特定產品的用戶? 我試過

UserModel.find({shoppingCart : product._id})... 
and 
UserModel.find({'shoppingCart._id' : product._id}).... 

但不幸的是它不起作用。有任何想法嗎?謝謝。

回答

0

您是否嘗試過...

如果使用查詢中的實際對象後,會自我滋潤成ID,並運行基於該對象ID搜索。

既然你有一個Schema類型爲「Schema.Types.ObjectId」如果你運行

UserModel.find({shoppingCart : product._id}) 

它將對product._id搜索爲「串」不OBJECTID。

+1

我認爲這應該是一個評論,除非擴展到一個完整的答案。 – MasterAM

+0

回答已更新,謝謝。 – tpae

相關問題