Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在mongoose中如何在$map之后立即使用$filter_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 在mongoose中如何在$map之后立即使用$filter

Node.js 在mongoose中如何在$map之后立即使用$filter,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,使用js代码时,我可以逐个使用函数表达式;例如: array.map(***).filter(...) 我可以在mongoose中使用上面那样的地图后使用过滤器吗 我的问题是这样的。我有一个如下的数据集: { "_id" : ObjectId("5e3bd328f3dec754e1b8e17d"), "userId" : "5e33ee0b4a3895a6d246f3ee", "userName" : "jackiewillen", "hasReviewedTimes" : 4, "notes

使用js代码时,我可以逐个使用函数表达式;例如:

array.map(***).filter(...)
我可以在mongoose中使用上面那样的地图后使用过滤器吗

我的问题是这样的。我有一个如下的数据集:

{
"_id" : ObjectId("5e3bd328f3dec754e1b8e17d"),
"userId" : "5e33ee0b4a3895a6d246f3ee",
"userName" : "jackiewillen",
"hasReviewedTimes" : 4,
"notes" : [ 
    {
        "time" : ISODate("2020-02-23T10:12:19.190Z"),
        "memoryLine" : [ 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51df83966daeae41e7f5b1"),
                "memoryTime" : ISODate("2020-02-23T10:42:19.190Z")
            }, 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51df83966daeae41e7f5b0"),
                "memoryTime" : ISODate("2020-02-23T22:12:19.190Z")
            }
        ]
    }, 
    {
        "time" : ISODate("2020-02-23T10:45:26.615Z"),
        "memoryLine" : [ 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51e746966daeae41e7f5bd"),
                "memoryTime" : ISODate("2020-02-23T11:15:26.615Z")
            }, 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51e746966daeae41e7f5bc"),
                "memoryTime" : ISODate("2020-02-23T22:45:26.615Z")
            }
        ]
    }, 
}
db.notes.aggregate([{
    $match: {
        "$and": [
            { userId: '5e33ee0b4a3895a6d246f3ee'}
        ]
    }
}, {
    $project: {
        notes: {
            $map: {
                input: "$notes",
                in: {
                    $mergeObjects: [
                        "$$this",
                        {
                            memoryLine: {
                                $filter: {
                                    input: "$$this.memoryLine",
                                    as: "mLine",
                                    cond: { $lt: ["$$mLine.memoryTime", new Date()] }
                                }
                            }
                        }
                    ]
                },
            },
        }
    }
}
])
我使用
$map
memoryLine
中获取比现在更少的
memoryTime
项目,如下所示:

{
"_id" : ObjectId("5e3bd328f3dec754e1b8e17d"),
"userId" : "5e33ee0b4a3895a6d246f3ee",
"userName" : "jackiewillen",
"hasReviewedTimes" : 4,
"notes" : [ 
    {
        "time" : ISODate("2020-02-23T10:12:19.190Z"),
        "memoryLine" : [ 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51df83966daeae41e7f5b1"),
                "memoryTime" : ISODate("2020-02-23T10:42:19.190Z")
            }, 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51df83966daeae41e7f5b0"),
                "memoryTime" : ISODate("2020-02-23T22:12:19.190Z")
            }
        ]
    }, 
    {
        "time" : ISODate("2020-02-23T10:45:26.615Z"),
        "memoryLine" : [ 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51e746966daeae41e7f5bd"),
                "memoryTime" : ISODate("2020-02-23T11:15:26.615Z")
            }, 
            {
                "hasReviewed" : false,
                "_id" : ObjectId("5e51e746966daeae41e7f5bc"),
                "memoryTime" : ISODate("2020-02-23T22:45:26.615Z")
            }
        ]
    }, 
}
db.notes.aggregate([{
    $match: {
        "$and": [
            { userId: '5e33ee0b4a3895a6d246f3ee'}
        ]
    }
}, {
    $project: {
        notes: {
            $map: {
                input: "$notes",
                in: {
                    $mergeObjects: [
                        "$$this",
                        {
                            memoryLine: {
                                $filter: {
                                    input: "$$this.memoryLine",
                                    as: "mLine",
                                    cond: { $lt: ["$$mLine.memoryTime", new Date()] }
                                }
                            }
                        }
                    ]
                },
            },
        }
    }
}
])
我的结果如下:

"notes": [
        {
            "time": "2020-02-23T10:12:19.190Z",
            "memoryLine": [
                {
                    "hasReviewed": false,
                    "_id": "5e51df83966daeae41e7f5b1",
                    "memoryTime": "2020-02-23T10:42:19.190Z"
                }
            ]
        },
        { //   =====> this item is not needed because of containing empty memoryLine
            "time": "2020-02-23T10:45:26.615Z",
            "memoryLine": [] //  =======> i dont want empty item
        },
   ]
但我想要这样的结果:

"notes": [
        {
            "time": "2020-02-23T10:12:19.190Z",
            "memoryLine": [
                {
                    "hasReviewed": false,
                    "_id": "5e51df83966daeae41e7f5b1",
                    "memoryTime": "2020-02-23T10:42:19.190Z"
                }
            ]
        }
   ]
因此,我在$map之后使用$filter来过滤包含空
memoryLine
的项:

db.notes.aggregate([{
    $match: {
        "$and": [
            { userId: '5e33ee0b4a3895a6d246f3ee'}
        ]
    }
}, {
    $project: {
        notes: {
            $map: {
                input: "$notes",
                in: {
                    $mergeObjects: [
                        "$$this",
                        {
                            memoryLine: {
                                $filter: {
                                    input: "$$this.memoryLine",
                                    as: "mLine",
                                    cond: { $lt: ["$$mLine.memoryTime", new Date()] }
                                }
                            }
                        }
                    ],
                    $filter: {
                      input: "$$this",
                      as: "note",
                      cond: { $ne: ["$$note.memoryLine", []] }
                    }
                },
            },
        }
    }
}

那么这就错了

您需要运行另一个
$filter
作为单独的管道阶段(为了可读性),或者作为当前
$project
的最外部阶段。我更喜欢第一个:

{
    $addFields: {
        notes: {
            $filter: {
                input: "$notes",
                cond: {
                    $ne: [ "$$this.memoryLine", [] ]
                }
            }
        }
    }
}