简体   繁体   English

如何使用猫鼬对深度嵌套的数组进行 $slice

[英]How can I $slice on a deeply nested array using mongoose

I'm trying to slice a deeply nested array.我正在尝试切片一个深度嵌套的数组。 Take the structure below as an example.以下面的结构为例。 I need to slice this array for pagination.我需要将此数组切片以进行分页。

{messages: [{ message: { members: [ {example: object, blah: blah}, {example2: object2, blah2: blah2} ] }]}

How would I use the slice operator in this scenario?在这种情况下我将如何使用切片运算符?

Here is an example of my current query shown below.这是我当前查询的示例,如下所示。

model.findOne({query: query}, { 'messages.0.message.members': { '$slice': [ 0, 10 ] } })

Then I return the document but the members length remains 14 when I'm slicing the array to return only the first 10 members of the array.然后我返回文档,但当我对数组进行切片以仅返回数组的前 10 个成员时,成员长度仍为 14。

This may not even be possible but I would like to include the slice in the query to limit any other logic that might slow it down.这甚至可能是不可能的,但我想在查询中包含切片以限制可能减慢它的任何其他逻辑。

Any help with this would be greatly appreciated.对此的任何帮助将不胜感激。 Thanks!谢谢!

I'm not sure whether it's easy or not to do it through .find() - As we know it's not working is because of embedded arrays, Please try this :我不确定通过.find()是否容易做到这.find() - 我们知道它不起作用是因为嵌入式数组,请试试这个:

db.YourCollectionName.aggregate([{ $match: { "_id": ObjectId("5df94e17400289966e8707a7") } },
/** You can use $addFields if you want to retain remaining fields */
{ $project: { 'messages': { $arrayElemAt: ["$messages", 0] } } },
{ $project: { members: '$messages.message.members' } },
{ $project: { membersArr: { '$slice': ['$members', 0, 2] } } }])

Result :结果 :

/* 1 */
{
    "_id" : ObjectId("5df94e17400289966e8707a7"),
    "membersArr" : [ 
        {
            "example" : "object",
            "blah" : "blah"
        }, 
        {
            "example2" : "object2",
            "blah2" : "blah2"
        }
    ]
}

Collection Data :收集数据:

/* 1 */
{
    "_id" : ObjectId("5df94e17400289966e8707a7"),
    "messages" : [ 
        {
            "message" : {
                "members" : [ 
                    {
                        "example" : "object",
                        "blah" : "blah"
                    }, 
                    {
                        "example2" : "object2",
                        "blah2" : "blah2"
                    }, 
                    {
                        "example2" : "object3",
                        "blah2" : "blah3"
                    }, 
                    {
                        "example2" : "object4",
                        "blah2" : "blah4"
                    }
                ]
            }
        }, 
        {
            "message" : {
                "members" : [ 
                    {
                        "example" : "object11",
                        "blah" : "blah11"
                    }, 
                    {
                        "example2" : "object211",
                        "blah2" : "blah211"
                    }, 
                    {
                        "example2" : "object311",
                        "blah2" : "blah311"
                    }, 
                    {
                        "example2" : "object411",
                        "blah2" : "blah411"
                    }
                ]
            }
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5df94e28400289966e870b34"),
    "messages" : [ 
        {
            "message" : {
                "members" : [ 
                    {
                        "example" : "objectF",
                        "blah" : "blah"
                    }, 
                    {
                        "example2" : "objectF2",
                        "blah2" : "blah2"
                    }, 
                    {
                        "example2" : "objectF3",
                        "blah2" : "blah3"
                    }, 
                    {
                        "example2" : "objectF4",
                        "blah2" : "blah4"
                    }
                ]
            }
        }
    ]
}

Ref : $addFields , $arrayElemAt , $slice参考: $addFields$arrayElemAt$slice

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM