我的数据库看起来像:
{email:"user", contacts:
[
{emailContact:"test", firstName:"test", lastName:"test", messages:
[
{email:"user", content:"hi", date:"ISODate(...)"}
{email:"test", content:"how are you?", date:"ISODate(...)"}
{email:"user", content:"im fine", date:"ISODate(...)"}
]
},
{emailContact:test2, firstName:"test2", lastName:"test2", messages:
[
{email:"user", content:"hahaha", date:"ISODate(...)"}
{email:"test2", content:"yea thats right", date:"ISODate(...)"}
{email:"user", content:"xd", date:"ISODate(...)"}
]
}
]
}
我必须得到特定索引的消息。
例如带有4、5、6索引的消息。
我已经试过几个类似的:
db.contacts.aggregation([{$match:{email:"elo@elo.pl"}},{$unwind:'$contacts'},{$match:{'contacts.emailContact':'user@user.pl'}},{$unwind:'$contacts.messages'},{$project:{email:1,content:1,date:{$slice:['$contacts.messages',4,6]}}},{$replaceRoot:{newRoot:'$contacts.messages'}}])
谢谢你的帮助