Mongodb 带字符串数组的mongoose聚合
我有如下格式的用户集合:Mongodb 带字符串数组的mongoose聚合,mongodb,mongoose,aggregate,Mongodb,Mongoose,Aggregate,我有如下格式的用户集合: [ { "_id": ObjectId("5f76ac1c337b875f3aff8cad"), "units": [ '1', '2', '3' ], "status": "true", "name": "xxx" }, { "_id": ObjectId(&
[
{
"_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 }]
}
})
此外,我还添加了对排序、选择字段、搜索文本(名称字段)和分页的支持。etc您有什么问题?['2',3']不在第三个文档中。它如何返回所有3个文档?你想解决的问题是什么?