我有一个mongo doc如下:
{
_id: "fe50fdee-4ea3-4824-94af-f369633c0c7a",
_class: "com.tracking.daoservice.model.TrackingData",
modified: ISODate("2014-09-10T23:38:48.407Z"),
eventtype: "William-Test",
eventdata: {
QueryDate: "01-APR-2014",
SearchQuery: {
keyword: "Java",
location: "Santa Clara, CA",
Facet: "skill~java~perl|workAuth~USC",
SearchAgentId: "4299"
},
Viewed: [
{
ViewedID: "8992ade400a",
Dockey: "3323aba3233",
PID: "32399a",
actionsTaken: "email|direct message|report seeker",
viewDate: "01-APR-2014",
MessageSent: "true",
Message: [
{
MessageID: "123aca323",
Delivered: "True",
Opened: "True",
ClickThroughRate: "NotBad",
MessageDate: "02-APR-2014",
Response: [
{
ResponseId: "a323a9da",
ResponseDate: "23-APR-2014"
}
]
}
]
}
]
},
eventsource: "API-Dev Test - JMachine",
sourceip: "myIp",
entityid: "TmoneyBunnyWunny",
groupid: "Dice",
datecreated: ISODate("2014-09-10T23:38:48.405Z")
}
我有一个关于mongodb的查询,以返回一组id的文档列表
db.TRACKING_DATA.find( {'eventdata.Viewed.Dockey': { $in: ['f7a16badfe6d2729449426ede1d7c558','3ede6f9f94df51bbf97fd9adf566f9d6','34508d254bb3393f52432963426eddc6','591297e6dc75af5aa43581ac66463562','c9fdf22ee4aef3e2f06a624cbafe01ec','b01cbe04b1c7b760b1ac50a953b182dd','e38d86d02a29730be5c7f18c16406fdd','2145ff57990a4d566dae99de05746536','febb0b44f6f481934e8c53a2c603b8a8','30d2ce2934ccbddcd78a16ea1b102765']}, 'entityid': '1026891'})
然而它会返回每个Dockey的所有实例,但是我只想为最近的每个Dockey返回一个,这可以从datecreated
属性中看到,有没有办法过滤关于查询的数据只返回这个,即内部查询,它只会为每个Dockey采用最新的文档。
答案 0 :(得分:0)
用户聚合管道:
db.TRACKING_DATA.aggregate([
{
$match : {
'eventdata.Viewed.Dockey' : {
$in : [ 'f7a16badfe6d2729449426ede1d7c558',
'3ede6f9f94df51bbf97fd9adf566f9d6',
'34508d254bb3393f52432963426eddc6',
'591297e6dc75af5aa43581ac66463562',
'c9fdf22ee4aef3e2f06a624cbafe01ec',
'b01cbe04b1c7b760b1ac50a953b182dd',
'e38d86d02a29730be5c7f18c16406fdd',
'2145ff57990a4d566dae99de05746536',
'febb0b44f6f481934e8c53a2c603b8a8',
'30d2ce2934ccbddcd78a16ea1b102765' ]
},
'entityid' : '1026891'
}
}, {
$sort : {
datecreated : -1
}
}, {
$group : {
_id : "$_id",
doc : {
$first : "$$ROOT" // this is whole document for each
// docKey with latest "datecreated"
}
}
} ]);