Mongoose 查询帮助:对嵌套数组进行分页、排序、限制
Posted
技术标签:
【中文标题】Mongoose 查询帮助:对嵌套数组进行分页、排序、限制【英文标题】:Mongoose Query Help: Paginate, Sort, Limit on Nested Array 【发布时间】:2016-09-23 00:56:09 【问题描述】:我有一个聊天猫鼬模型,下面是示例数据。如果这仍然不清楚,请回复我您的问题。非常感谢任何帮助。
"_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)
下面是解释:
-
user1分别在11:36和11:38发送了2条消息
user2分别在11:46和11:48发送了2条消息
user3 分别在 11:56 和 11:58 发送了 2 条消息
user4 分别在 11:66 和 11:68 发送了 2 条消息
我的预期结果是:
分页/限制条件:
-
每页显示 2 条记录。
仅显示基于用户的最新消息。
示例输出:
第 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?"
【问题讨论】:
【参考方案1】:试试这种查询方式,对你有帮助
查询第一页
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' ])
【讨论】:
感谢您的反馈。下一页的小改动。应该改变 $limit 和 $skip 的顺序,以便 $skip 排在第一位,$limit 排在第二位。 $skip:1, $limit:1 没错。我只是发布了答案,但我没有检查过 有人可以回答如何使用猫鼬做同样的事情吗? @SyedSouban 我希望这个链接对你有帮助 excellencenodejsblog.com/… ,不需要在查询中改变任何东西,只需创建一个收集模型然后使用模型名称而不是把这个“db.getCollection(message )"以上是关于Mongoose 查询帮助:对嵌套数组进行分页、排序、限制的主要内容,如果未能解决你的问题,请参考以下文章