{
"_id" : ObjectId("5745910831a1sd58d070a8faa"),
"messages" : [
{
"user" : "user1",
"message" : "How are you user1?",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:36:00.468+0000"),
"_id" : ObjectId("5745912c31a1c58d070a904d")
},
{
"user" : "user1",
"message" : "Hello user1",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:38:53.893+0000"),
"_id" : ObjectId("5745912531a1c58d070a902e")
}
],
"createDate" : ISODate("2016-05-25T11:35:20.534+0000"),
"users" : [
"57450b4506561ff5052f0a66",
"57450d8108d8d22c06cf138f"
],
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("57458e9331a1c58d070a8e30"),
"messages" : [
{
"user" : "user2",
"message" : "How are you user2",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:46:03.240+0000"),
"_id" : ObjectId("574590f331a1c58d070a8ede")
},
{
"user" : "user2",
"message" : "Hello user2",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:48:53.925+0000"),
"_id" : ObjectId("574590e931a1c58d070a8eab")
}
],
"createDate" : ISODate("2016-05-25T11:35:20.534+0000"),
"users" : [
"5745149e3aaab38706c00b64",
"57450d8108d8d22c06cf138f"
],
"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5745910831a1c58d070a8faa"),
"messages" : [
{
"user" : "user3",
"message" : "How are you user3?",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:56:00.468+0000"),
"_id" : ObjectId("5745912c31a1c58d070a904d")
},
{
"user" : "user3",
"message" : "Hello user3",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:58:53.893+0000"),
"_id" : ObjectId("5745912531a1c58d070a902e")
}
],
"createDate" : ISODate("2016-05-25T11:35:20.534+0000"),
"users" : [
"57450b4506561ff5052f0a66",
"57450d8108d8d22c06cf138f"
],
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("5745910831a1c58d070a8faa"),
"messages" : [
{
"user" : "user4",
"message" : "How are you user4?",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:66:00.468+0000"),
"_id" : ObjectId("5745912c31a1c58d070a904d")
},
{
"user" : "user4",
"message" : "Hello user4",
"readInd" : "N",
"createDate" : ISODate("2016-05-25T11:68:53.893+0000"),
"_id" : ObjectId("5745912531a1c58d070a902e")
}
],
"createDate" : ISODate("2016-05-25T11:35:20.534+0000"),
"users" : [
"57450b4506561ff5052f0a66",
"57450d8108d8d22c06cf138f"
],
"__v" : NumberInt(0)
}
下面是解释:
我的预期结果是:
分页/限制标准:
示例输出:
第1页:
"user" : "57450d8108d8d22c06cf138f",
"message" : "How are you user4?"
"user" : "57450d8108d8d22c06cf138f",
"message" : "How are you user3?"
第2页:
"user" : "57450d8108d8d22c06cf138f",
"message" : "How are you user2"
"user" : "57450d8108d8d22c06cf138f",
"message" : "How are you user1?"
答案 0 :(得分:0)
尝试这种查询方式,这对你有帮助
查询第一页
db.getCollection('message').aggregate( [ { $match : { user : "57450d8108d8d22c06cf138f" } },
{ $unwind : "$messages" } ,
{ $sort : { 'messages.createDate' : -1} },
{ $limit : 2 },
{ $project : { _id: 0,'message':'$messages.message','user':'$messages.user'} } ])
查询下一页
db.getCollection('message').aggregate( [ { $match : { user : "57450d8108d8d22c06cf138f" } },
{ $unwind : "$messages" } ,
{ $sort : { 'messages.createDate' : -1} },
{ $limit : 2 },{ $skip : 2 }
{ $project : { _id: 0,'message':'$messages.message','user':'$messages.user'} } ])