在MongoDB中只获取一次Value

时间:2015-01-20 14:10:43

标签: python mongodb

我的MongoDB中有一些文档。这些是产品。每隔xx小时/天对产品进行爬网。现在我一遍又一遍地使用相同的产品,但它们上面有不同的时间戳。很快价格也会发生变化。

根据ModelID查找()产品的每个产品的价格历史记录。 ModelID是每个产品的独特之处。这是问题所在:

我如何获得“每件产品(ModelID)”1文件。

为什么需要这个? 我想获得产品名称BY ModelID,所以我可以在网站上显示数据库中有哪些产品。

所以这是我的Collection的一个例子:CPU:

  

db.CPU.find()

{ "_id" : ObjectId("54bc38cd02cebca524c4acc6"), "Price" : 329.95, "Name" : "Intel Core i7 4790K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I74790K", "Timestamp" : 1421621453 }
{ "_id" : ObjectId("54bc38d402cebca524c4acc7"), "Price" : 194.95, "Name" : "Intel Core i5 4670", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I54670", "Timestamp" : 1421621460 }
{ "_id" : ObjectId("54bc38d602cebca524c4acc8"), "Price" : 164.95, "Name" : "AMD FX-8350 Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "FD8350FRHKBOX", "Timestamp" : 1421621462 }
{ "_id" : ObjectId("54bc38d802cebca524c4acc9"), "Price" : 209.95, "Name" : "Intel Core i5 4690", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I54690", "Timestamp" : 1421621464 }
{ "_id" : ObjectId("54bc38db02cebca524c4acca"), "Price" : 188.95, "Name" : "Intel Core i5 4590", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I54590", "Timestamp" : 1421621467 }
{ "_id" : ObjectId("54bc38dd02cebca524c4accb"), "Price" : 319.95, "Name" : "Intel Core i7 3770K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80637I73770K", "Timestamp" : 1421621469 }
{ "_id" : ObjectId("54bc38df02cebca524c4accc"), "Price" : 84.95, "Name" : "AMD FX-4300", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "FD4300WMHKBOX", "Timestamp" : 1421621471 }
{ "_id" : ObjectId("54bc38e102cebca524c4accd"), "Price" : 99.95, "Name" : "AMD FX-6300 Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "FD6300WMHKBOX", "Timestamp" : 1421621473 }
{ "_id" : ObjectId("54bc38e302cebca524c4acce"), "Price" : 549.95, "Name" : "Intel Core I7 5930K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80648I75930K", "Timestamp" : 1421621475 }
{ "_id" : ObjectId("54bc38e502cebca524c4accf"), "Price" : 189.95, "Name" : "Intel Core i5 4460", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I54460", "Timestamp" : 1421621477 }
{ "_id" : ObjectId("54bc38e702cebca524c4acd0"), "Price" : 68.95, "Name" : "Intel Pentium G3258 Anniversary Edition", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646G3258", "Timestamp" : 1421621479 }
{ "_id" : ObjectId("54bc38e902cebca524c4acd1"), "Price" : 194.95, "Name" : "Intel Core i5 4570", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I54570", "Timestamp" : 1421621481 }
{ "_id" : ObjectId("54bc38ec02cebca524c4acd2"), "Price" : 84.95, "Name" : "AMD A8-5600K Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD560KWOHJBOX", "Timestamp" : 1421621484 }
{ "_id" : ObjectId("54bc38ee02cebca524c4acd3"), "Price" : 119.95, "Name" : "AMD A10-6790K Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD679KWOHLBOX", "Timestamp" : 1421621486 }
{ "_id" : ObjectId("54bc38f002cebca524c4acd4"), "Price" : 104.95, "Name" : "AMD A10-5800K", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD580KWOHJBOX", "Timestamp" : 1421621488 }
{ "_id" : ObjectId("54bc38f202cebca524c4acd5"), "Price" : 543.95, "Name" : "Intel Core i7 4930K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80633I74930K", "Timestamp" : 1421621490 }
{ "_id" : ObjectId("54bc38f402cebca524c4acd6"), "Price" : 184.95, "Name" : "Intel Core i5 4440", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I54440", "Timestamp" : 1421621492 }
{ "_id" : ObjectId("54bc38f602cebca524c4acd7"), "Price" : 95.95, "Name" : "AMD A8-6600K", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD660KWOHLBOX", "Timestamp" : 1421621494 }
{ "_id" : ObjectId("54bc38f902cebca524c4acd8"), "Price" : 159.95, "Name" : "AMD A10-7850K Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD785KXBJABOX", "Timestamp" : 1421621497 }
{ "_id" : ObjectId("54bc38fb02cebca524c4acd9"), "Price" : 59.95, "Name" : "AMD A6-6400K", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD640KOKHLBOX", "Timestamp" : 1421621499 }
  

输入“it”了解更多信息   它

{ "_id" : ObjectId("54bc38fd02cebca524c4acda"), "Price" : 69.95, "Name" : "Intel Pentium G3450", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646G3450", "Timestamp" : 1421621501 }
{ "_id" : ObjectId("54bc38ff02cebca524c4acdb"), "Price" : 849.95, "Name" : "Intel Core i7 4960X", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80633I74960X", "Timestamp" : 1421621503 }
{ "_id" : ObjectId("54bc390202cebca524c4acdc"), "Price" : 309.95, "Name" : "Intel Core i7 4820K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80633I74820K", "Timestamp" : 1421621506 }
{ "_id" : ObjectId("54bc390402cebca524c4acdd"), "Price" : 295.95, "Name" : "Intel Core i7 4770", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I74770", "Timestamp" : 1421621508 }
{ "_id" : ObjectId("54bc390802cebca524c4acde"), "Price" : 154.95, "Name" : "Intel Core i3 4370", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I34370", "Timestamp" : 1421621512 }
{ "_id" : ObjectId("54bc390a02cebca524c4acdf"), "Price" : 139.95, "Name" : "Intel Core i3 4350", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I34350", "Timestamp" : 1421621514 }
{ "_id" : ObjectId("54bc390c02cebca524c4ace0"), "Price" : 129.95, "Name" : "Intel Core i3 4330", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I34330", "Timestamp" : 1421621516 }
{ "_id" : ObjectId("54bc390e02cebca524c4ace1"), "Price" : 122.95, "Name" : "Intel Core i3 4160", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I34160", "Timestamp" : 1421621518 }
{ "_id" : ObjectId("54bc391102cebca524c4ace2"), "Price" : 119.95, "Name" : "Intel Core i3 4150", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I34150", "Timestamp" : 1421621521 }
{ "_id" : ObjectId("54bc391302cebca524c4ace3"), "Price" : 119.95, "Name" : "Intel Core i3 4130", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I34130", "Timestamp" : 1421621523 }
{ "_id" : ObjectId("54bc391602cebca524c4ace4"), "Price" : 274.95, "Name" : "AMD FX-9590 Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "FD9590FHHKWOF", "Timestamp" : 1421621526 }
{ "_id" : ObjectId("54bc391702cebca524c4ace5"), "Price" : 199.95, "Name" : "AMD FX-9370 Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "FD9370FHHKWOF", "Timestamp" : 1421621527 }
{ "_id" : ObjectId("54bc391a02cebca524c4ace6"), "Price" : 88.95, "Name" : "AMD A8-6500", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD6500OKHLBOX", "Timestamp" : 1421621530 }
{ "_id" : ObjectId("54bc391c02cebca524c4ace7"), "Price" : 84.95, "Name" : "AMD A6-7400K Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD740KYBJABOX", "Timestamp" : 1421621532 }
{ "_id" : ObjectId("54bc391e02cebca524c4ace8"), "Price" : 54.95, "Name" : "AMD A6-5400K", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD540KOKHJBOX", "Timestamp" : 1421621534 }
{ "_id" : ObjectId("54bc392002cebca524c4ace9"), "Price" : 134.95, "Name" : "AMD A10-7700K Black Edition", "Type" : "CPU", "Brand" : "AMD", "ModelID" : "AD770KXBJABOX", "Timestamp" : 1421621536 }
{ "_id" : ObjectId("54bc392302cebca524c4acea"), "Price" : 229.95, "Name" : "Intel Core i5 3570K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80637I53570K", "Timestamp" : 1421621539 }
{ "_id" : ObjectId("54bc63a702cebca816a786e3"), "Timestamp" : 1421632423, "Name" : "Intel Core i7 4790K", "Price" : 329.95, "Brand" : "Intel", "Type" : "CPU", "ModelID" : "BX80646I74790K" }
{ "_id" : ObjectId("54bc63aa02cebca816a786e4"), "Timestamp" : 1421632426, "Name" : "Intel Core i5 4670", "Price" : 194.95, "Brand" : "Intel", "Type" : "CPU", "ModelID" : "BX80646I54670" }
{ "_id" : ObjectId("54bc66ec02cebca8b84a7eef"), "Price" : 329.95, "Timestamp" : 1421633260, "ModelID" : "BX80646I74790K", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i7 4790K" }
  

输入“it”了解更多信息   它

{ "_id" : ObjectId("54bc66f002cebca8b84a7ef0"), "Price" : 194.95, "Timestamp" : 1421633264, "ModelID" : "BX80646I54670", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4670" }
{ "_id" : ObjectId("54bc66f302cebca8b84a7ef1"), "Price" : 164.95, "Timestamp" : 1421633267, "ModelID" : "FD8350FRHKBOX", "Brand" : "AMD", "Type" : "CPU", "Name" : "AMD FX-8350 Black Edition" }
{ "_id" : ObjectId("54bc66f602cebca8b84a7ef2"), "Price" : 209.95, "Timestamp" : 1421633270, "ModelID" : "BX80646I54690", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4690" }
{ "_id" : ObjectId("54bc66f802cebca8b84a7ef3"), "Price" : 188.95, "Timestamp" : 1421633272, "ModelID" : "BX80646I54590", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4590" }
{ "_id" : ObjectId("54bc66fa02cebca8b84a7ef4"), "Price" : 319.95, "Timestamp" : 1421633274, "ModelID" : "BX80637I73770K", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i7 3770K" }
{ "_id" : ObjectId("54bc66fc02cebca8b84a7ef5"), "Price" : 84.95, "Timestamp" : 1421633276, "ModelID" : "FD4300WMHKBOX", "Brand" : "AMD", "Type" : "CPU", "Name" : "AMD FX-4300" }
{ "_id" : ObjectId("54bc66fe02cebca8b84a7ef6"), "Price" : 99.95, "Timestamp" : 1421633278, "ModelID" : "FD6300WMHKBOX", "Brand" : "AMD", "Type" : "CPU", "Name" : "AMD FX-6300 Black Edition" }
{ "_id" : ObjectId("54bc670002cebca8b84a7ef7"), "Price" : 549.95, "Timestamp" : 1421633280, "ModelID" : "BX80648I75930K", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core I7 5930K" }
{ "_id" : ObjectId("54bc670202cebca8b84a7ef8"), "Price" : 189.95, "Timestamp" : 1421633282, "ModelID" : "BX80646I54460", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4460" }
{ "_id" : ObjectId("54bc674c02cebca8d0ccf375"), "ModelID" : "BX80646I74790K", "Timestamp" : 1421633356, "Type" : "CPU", "Brand" : "Intel", "Price" : 329.95, "Name" : "Intel Core i7 4790K" }
{ "_id" : ObjectId("54bc674d02cebca8d0ccf376"), "ModelID" : "BX80646I54670", "Timestamp" : 1421633357, "Type" : "CPU", "Brand" : "Intel", "Price" : 194.95, "Name" : "Intel Core i5 4670" }
{ "_id" : ObjectId("54bc674f02cebca8d0ccf377"), "ModelID" : "FD8350FRHKBOX", "Timestamp" : 1421633359, "Type" : "CPU", "Brand" : "AMD", "Price" : 164.95, "Name" : "AMD FX-8350 Black Edition" }
{ "_id" : ObjectId("54bc675202cebca8d0ccf378"), "ModelID" : "BX80646I54690", "Timestamp" : 1421633362, "Type" : "CPU", "Brand" : "Intel", "Price" : 209.95, "Name" : "Intel Core i5 4690" }
{ "_id" : ObjectId("54bc689f02cebca9626f7e27"), "ModelID" : "BX80646I74790K", "Timestamp" : 1421633695, "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i7 4790K", "Price" : 329.95 }
{ "_id" : ObjectId("54bc68a102cebca9626f7e28"), "ModelID" : "BX80646I54670", "Timestamp" : 1421633697, "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4670", "Price" : 194.95 }
{ "_id" : ObjectId("54bc68a402cebca9626f7e29"), "ModelID" : "FD8350FRHKBOX", "Timestamp" : 1421633700, "Brand" : "AMD", "Type" : "CPU", "Name" : "AMD FX-8350 Black Edition", "Price" : 164.95 }
{ "_id" : ObjectId("54bc68a602cebca9626f7e2a"), "ModelID" : "BX80646I54690", "Timestamp" : 1421633702, "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4690", "Price" : 209.95 }
{ "_id" : ObjectId("54bc68a802cebca9626f7e2b"), "ModelID" : "BX80646I54590", "Timestamp" : 1421633704, "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i5 4590", "Price" : 188.95 }
{ "_id" : ObjectId("54bc696002cebca99c34f39c"), "Brand" : "Intel", "Name" : "Intel Core i7 4790K", "Price" : 329.95, "Timestamp" : 1421633888, "ModelID" : "BX80646I74790K", "Type" : "CPU" }
{ "_id" : ObjectId("54bc696202cebca99c34f39d"), "Brand" : "Intel", "Name" : "Intel Core i5 4670", "Price" : 194.95, "Timestamp" : 1421633890, "ModelID" : "BX80646I54670", "Type" : "CPU" }
  

输入“it”了解更多信息   它

{ "_id" : ObjectId("54bc696402cebca99c34f39e"), "Brand" : "AMD", "Name" : "AMD FX-8350 Black Edition", "Price" : 164.95, "Timestamp" : 1421633892, "ModelID" : "FD8350FRHKBOX", "Type" : "CPU" }
{ "_id" : ObjectId("54bc696602cebca99c34f39f"), "Brand" : "Intel", "Name" : "Intel Core i5 4690", "Price" : 209.95, "Timestamp" : 1421633894, "ModelID" : "BX80646I54690", "Type" : "CPU" }
{ "_id" : ObjectId("54bc696802cebca99c34f3a0"), "Brand" : "Intel", "Name" : "Intel Core i5 4590", "Price" : 188.95, "Timestamp" : 1421633896, "ModelID" : "BX80646I54590", "Type" : "CPU" }

所以,如果我只想拥有ModelID的产品:BX80646I74790K

  

db.CPU.find({ModelID:'BX80646I74790K'})

{ "_id" : ObjectId("54bc38cd02cebca524c4acc6"), "Price" : 329.95, "Name" : "Intel Core i7 4790K", "Type" : "CPU", "Brand" : "Intel", "ModelID" : "BX80646I74790K", "Timestamp" : 1421621453 }
{ "_id" : ObjectId("54bc63a702cebca816a786e3"), "Timestamp" : 1421632423, "Name" : "Intel Core i7 4790K", "Price" : 329.95, "Brand" : "Intel", "Type" : "CPU", "ModelID" : "BX80646I74790K" }
{ "_id" : ObjectId("54bc66ec02cebca8b84a7eef"), "Price" : 329.95, "Timestamp" : 1421633260, "ModelID" : "BX80646I74790K", "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i7 4790K" }
{ "_id" : ObjectId("54bc674c02cebca8d0ccf375"), "ModelID" : "BX80646I74790K", "Timestamp" : 1421633356, "Type" : "CPU", "Brand" : "Intel", "Price" : 329.95, "Name" : "Intel Core i7 4790K" }
{ "_id" : ObjectId("54bc689f02cebca9626f7e27"), "ModelID" : "BX80646I74790K", "Timestamp" : 1421633695, "Brand" : "Intel", "Type" : "CPU", "Name" : "Intel Core i7 4790K", "Price" : 329.95 }
{ "_id" : ObjectId("54bc696002cebca99c34f39c"), "Brand" : "Intel", "Name" : "Intel Core i7 4790K", "Price" : 329.95, "Timestamp" : 1421633888, "ModelID" : "BX80646I74790K", "Type" : "CPU" }

但我只想要一次,我该怎么做?

1 个答案:

答案 0 :(得分:1)

您应该使用distinct。尝试使用:

db.CPU.distinct( 'ModelID' )