2017-08-29 41 views
0

我想查詢這個Mongodb對象,但似乎無法獲得我需要的某些數據點。我該如何說出imagePath數據?數據保存在一個稱爲訂單的表格中。我嘗試過點符號,即order.cart [0],但無法弄清楚如何在數組中更深入。如何查詢這個Mongodb對象?

{ 
    "_id": ObjectId("5976b6b11306910658b1ff57"), 
    "address": "6210 place", 
    "name": "frank", 
    "paymentId": "ch_1AjJRVDfJryYeuMpJC80cp5k", 
    "email": "[email protected]", 
    "time": ISODate("2017-07-25T03:10:41.522Z"), 
    "cart": [ 
     { 
      "items": { 
       "5975228a215c0f074b64f58e": { 
        "item": { 
         "_id": "5975228a215c0f074b64f58e", 
         "title": "Bracelet 3", 
         "imagePath": "https://www.costco.com/wcsstore/CostcoUSBCCatalogAssetStore/category-tiles/pearl-bracelets.jpg", 
         "description": "This is bracelet 3", 
         "price": 12, 
         "__v": 0 
        }, 
        "qty": 1, 
        "price": 12 
       }, 
       "59752242215c0f074b64f58c": { 
        "item": { 
         "_id": "59752242215c0f074b64f58c", 
         "title": "Bracelet 1", 
         "imagePath": "https://img0.etsystatic.com/160/0/12655872/il_340x270.1187191078_i2ha.jpg", 
         "description": "This is bracelet 1", 
         "price": 10, 
         "__v": 0 
        }, 
        "qty": 2, 
        "price": 20 
       }, 
       "5975226a215c0f074b64f58d": { 
        "item": { 
         "_id": "5975226a215c0f074b64f58d", 
         "title": "Bracelet 2", 
         "imagePath": "http://media.tiffany.com/is/image/Tiffany/EcomBrowseM/paloma-picasso-knot-bead-bracelet-34946183_963148_ED.jpg?op_usm=1.00,1.00,6.00&defaultImage=NoImageAvailable&&", 
         "description": "This is bracelet 2", 
         "price": 5, 
         "__v": 0 
        }, 
        "qty": 1, 
        "price": 5 
       } 
      }, 
      "totalQty": 4, 
      "totalPrice": 37 
     } 
    ], 
    "__v": 0 
} 
+0

你可以顯示你迄今爲止嘗試過的代碼嗎?你想做什麼?我正在猜測通過購物車[0] .items循環? – bluetoft

+0

實際的「點符號」是「cart.0」,用於向MongoDB發出「查詢」,這與JavaScript語法不同。真的不清楚你想要做什麼「查詢」,因爲你實際上沒有說出來。你的內容「'項目」'是一個問題。您已經使用「命名鍵」而不是製作數組的「項目」成員。實際上,它看起來像''cart''已經在** error **中做成了一個單一的元素數組,實際上你希望這個數組是「項目」。或者至少這是結構「應該」取而代之的。 –

回答

0

const data = { 
 
    "address": "6210 place", 
 
    "name": "frank", 
 
    "paymentId": "ch_1AjJRVDfJ ryYeuMpJC80cp5k", 
 
    "email": "[email protected]", 
 
    "cart": [ 
 
     { 
 
      "items": { 
 
       "59752 28a215c0f074b64f58e": { 
 
        "item": { 
 
         "_id": "5975228a215c0f074b64f58e", 
 
         "title": "Bracelet 3", 
 
         "imagePath": "https:// www.costco.com/wcsstore/CostcoUSBCCatalogAssetStore/category-tiles/pearl-bracelets.jpg", 
 
         "description": "This is brace let 3", 
 
         "price": 12, 
 
         "__v": 0 
 
        }, 
 
        "qty": 1, 
 
        "price": 12 
 
       }, 
 
       "59752242215c0f074b64f58c": { 
 
        "item": { 
 
         "_id": "597522 42215c0f074b64f58c", 
 
         "title": "Bracelet 1", 
 
         "imagePath": "https://img0.etsystatic.com/160/0/12655872/il_340x270.11871 91078_i2ha.jpg", 
 
         "description": "This is bracelet 1", 
 
         "price": 10, 
 
         "__v": 0 
 
        }, 
 
        "qty": 2, 
 
        "price": 20 
 
       }, 
 
       "5975226a2 15c0f074b64f58d": { 
 
        "item": { 
 
         "_id": "5975226a215c0f074b64f58d", 
 
         "title": "Bracelet 2", 
 
         "imagePath": "http://media .tiffany.com/is/image/Tiffany/EcomBrowseM/paloma-picasso-knot-bead-bracelet-34946183_963148_ED.jpg?op_usm=1.00,1.00,6.0 0&defaultImage=NoImageAvailable&&", 
 
         "description": "This is bracelet 2", 
 
         "price": 5, 
 
         "__v": 0 
 
        }, 
 
        "qty": 1, 
 
        "price": 5 
 
       } 
 
      }, 
 
      "totalQty": 4, 
 
      "totalPrice": 37 
 
     } 
 
    ], 
 
    "__v": 0 
 
}; 
 

 
console.log('looping through Object.keys') 
 
Object.keys(data.cart[0].items).forEach(key => { 
 
    const cartItem = data.cart[0].items[key]; 
 
    console.log('cartItem.item.imagePath', cartItem.item.imagePath) 
 
}) 
 

 
console.log('looping through Object.values') 
 
Object.values(data.cart[0].items).forEach(cartItem=> { 
 
    
 
    console.log('cartItem.item.imagePath', cartItem.item.imagePath) 
 
})

由於您的購物車item.items是一個對象。您可以利用Object.keysObject.values來遍歷對象屬性。

+0

這工作!我能夠正確地循環。感謝您的快速回復,甚至更少的信息(對不起,我是新手),但再次感謝! – cisco

+0

@cisco查看我更新的答案...你也可以利用Object.values(可能更容易) – bluetoft