这是我的收藏品
print (df.dtypes)
Date object
val1 float64
val2 float64
dtype: object
df.Date = pd.to_datetime(df.Date)
print (df.dtypes)
Date datetime64[ns]
val1 float64
val2 float64
dtype: object
预期输出:
[
{
_id: "585be0da13385513689f704a",
site: "NBT",
url: "m.nbt.com",
label: "NBT Home",
platform: "mobile",
speed: 61,
date: "2016-12-22T14:18:57.975Z"
},
{
_id: "585be0da13385513689f704b",
site: "NBT",
url: "nbt.com",
label: "NBT Home",
platform: "desktop",
speed: 75,
date: "2016-12-22T14:15:57.975Z"
},
{
_id: "585be0da13385513689f704c",
site: "MT",
url: "m.mt.com",
label: "MT Home",
platform: "mobile",
speed: 40,
date: "2016-12-22T14:01:57.975Z"
},
{
_id: "585be0da13385513689f704d",
site: "NBT",
url: "m.nbt.com",
label: "NBT Home",
platform: "mobile",
speed: 90,
date: "2016-12-22T12:18:57.975Z"
}
]
基本上我想要首先匹配不同的(标签+平台)组合文档。
正如你在colloection中看到的那样,标签-NBT Home存在于3个文件中,但我想要只检索标签+平台的唯一组合。因此NBT Home for mobile和NBT Home for desktop正是我所期待的。
请帮忙。
答案 0 :(得分:1)
试试这个:
db.collection.aggregate([
{
$group:{
_id:{
label:"$label",
platform:"$platform"
},
site:{
$first:"$site"
},
url:{
$first:"$url"
},
speed:{
$first:"$speed"
}
}
}
])
输出:
{
"_id" : {
"label" : "MT Home",
"platform" : "mobile"
},
"site" : "MT",
"url" : "m.mt.com",
"speed" : 40
}
{
"_id" : {
"label" : "NBT Home",
"platform" : "desktop"
},
"site" : "NBT",
"url" : "nbt.com",
"speed" : 75
}
{
"_id" : {
"label" : "NBT Home",
"platform" : "mobile"
},
"site" : "NBT",
"url" : "m.nbt.com",
"speed" : 61
}
答案 1 :(得分:-1)
在汇总管道中使用$group,然后使用$first或$limit
以下查询是使用$ first编写的。
db.collection.aggregate([{$group:{ "_id":{"label":"$label", "platform":"$platform"}, "site": {"$first":"$site"}, "url": {"$first":"$url"}, "speed":{"$first":"$speed"}, "date":{"$first":"$date"} } }])
对于具有以下记录的样本集合
db.collection.find()
{ "_id" : "585be0da13385513689f704a", "site" : "NBT", "url" : "m.nbt.com", "labe
l" : "NBT Home", "platform" : "mobile", "speed" : 61, "date" : "2016-12-22T14:18
:57.975Z" }
{ "_id" : "585be0da13385513689f704b", "site" : "NBT", "url" : "nbt.com", "label"
: "NBT Home", "platform" : "desktop", "speed" : 75, "date" : "2016-12-22T14:15:
57.975Z" }
{ "_id" : "585be0da13385513689f704c", "site" : "MT", "url" : "m.mt.com", "label"
: "MT Home", "platform" : "mobile", "speed" : 40, "date" : "2016-12-22T14:01:57
.975Z" }
{ "_id" : "585be0da13385513689f704d", "site" : "NBT", "url" : "m.nbt.com", "labe
l" : "NBT Home", "platform" : "mobile", "speed" : 90, "date" : "2016-12-22T12:18
:57.975Z" }
上述查询将结果显示为
{
"_id": {
"label": "MT Home",
"platform": "mobile"
},
"site": "MT",
"url": "m.mt.com",
"speed": 40,
"date": "2016-12-22T14:01:57.975Z"
}
{
"_id": {
"label": "NBT Home",
"platform": "mobile"
},
"site": "NBT",
"url": "m.nbt.com",
"speed": 61,
"date": "2016-12-22T14:18:57.975Z"
}
{
"_id": {
"label": "NBT Home",
"platform": "desktop"
},
"site": "NBT",
"url ": "nbt.com",
"speed": 75,
"date": "2016-12-22T14:15:57.975Z"
}