猫鼬聚合与字符串数组

时间:2020-11-03 02:15:14

标签: mongodb mongoose aggregate

我有一个类似以下格式的用户集合:

[
  {
    "_id": ObjectId("5f76ac1c337b875f3aff8cad"),
    "units": [ '1', '2', '3' ],
    "status": "true",
    "name": "xxx"
  },
  {
    "_id": ObjectId("5f76ac1c337b875f3aff8cad"),
    "units": [ '2', '3', '4' ],
    "status": "true",
    "name": "yyy"
  },
  {
    "_id": ObjectId("5f76ac1c337b875f3aff8cad"),
    "units": ['4', '1', '5' ],
    "status": "true",
    "name": "zzz"
  }
]

我正在尝试查找如下查询 输入:['2','3'],应返回所有3条记录。

我有一个单位列表,例如['2'],应该返回前2条记录。

我正在使用汇总查询,因为需要记录总数。

const id_lists = ['1', '2'];    
User.aggregate(    
    {"$match": { "status" : "true" , "units": { "$in": id_lists } },
    {
      '$facet': {
        metadata: [{ $count: "total" }],
        users: [{ $skip: 0 }, { $limit: 10 }]
      }
    })

我还要添加对排序的支持,选择字段,搜索文本(名称字段)和分页。等等

0 个答案:

没有答案