Javascript 避免在mongodb和express生成的已访问URL上循环

Javascript 避免在mongodb和express生成的已访问URL上循环,javascript,node.js,mongodb,express,cookies,Javascript,Node.js,Mongodb,Express,Cookies,我有一个mongodb集合,其中每个文档都可以通过特定的url访问 目标是向用户显示一个以前从未访问过的随机文档url,直到用户看到集合中的所有文档,然后应“清除”整个集合,以便再次访问 我曾想过使用cookies来实现它,但我还没有找到一种方法来实现它 该应用程序使用express for nodejs构建,mongodb使用mongoose模块 Model.class: 来自路由器的代码片段: 如何查找数据库条目 如果您按顺序循环浏览文档,而不是随机浏览,则只需在当前id中添加+X即可 如果

我有一个mongodb集合,其中每个文档都可以通过特定的url访问

目标是向用户显示一个以前从未访问过的随机文档url,直到用户看到集合中的所有文档,然后应“清除”整个集合,以便再次访问

我曾想过使用cookies来实现它,但我还没有找到一种方法来实现它

该应用程序使用express for nodejs构建,mongodb使用mongoose模块

Model.class:

来自路由器的代码片段:

如何查找数据库条目


如果您按顺序循环浏览文档,而不是随机浏览,则只需在当前id中添加+X即可

如果是随机的,您需要为所有用户存储他们已经看到的所有文档。因此,您需要在数据库中添加另一个“表”,或者需要在用户模型中添加一个字段,用于存储所有文档

“最好”的解决方案是,现在就考虑在用户模型中添加此字段,或者能够知道IP X已经看到文档A和B。当用户尝试访问您的页面时,您将获得您拥有的文档的所有id列表,删除用户看到的id,并在此列表中随机执行操作

dilemmaRouter.route('/next')
    .get(function (req, res) {

        Dilemma.count().exec(function (err, count) {
           // find all documents 
            User.find({'idUserOrIP' : 'userIPorID'}).exec(function(user) {
                var userListSaw = user.listSaw;
            })
            // create a list with all your document id
            var allDocs = [1...100];
            // remove id already seen (user saw id 1 to 3)
            allDocs = [4...100];
            // random now store the index of the id of the document you want to display
            var random = Math.floor(Math.random() * allDocs.length);
             // just find your document with the id you just get
            Dilemma.find({'id' : allDocs[random]}).exec(function (err, dilemma) { //This function is supposed to redirect to an unvisited URL, and mark it as visited
                dilemmaID = dilemma._id;

                res.redirect('/' + dilemma.id + '/' + dilemma.slug);

            })
        })
})

目前我没有任何用户模型,因为它将主要是访问我的网站的客人。但如果我要做一个,我怎么才能抓取一个用户的ip地址,并从中创建一个用户呢?另外,你不认为用cookies在某种程度上是可行的吗?对我来说,这似乎是最好的选择,如果可能的话。顺便说一句,我用一段代码片段更新了我的帖子,说明了数据库条目的URL是如何生成的。关于ip,你可以从req访问它。但是你也可以用饼干,它会很好的,很高兴知道。你知道我该怎么做饼干吗?是否可以创建特定于url的Cookie?例如,我可以为所有访问过的页面设置cookie='visited'。在用户cookie中,只存储所有访问过的页面的列表。您可以使用cookie解析器中间件来处理cookie
dilemmaRouter.route('/next')
    .get(function (req, res) {

        Dilemma.count().exec(function (err, count) {
            var random = Math.floor(Math.random() * count);

            Dilemma.findOne().skip(random).exec(function (err, dilemma) { //This function is supposed to redirect to an unvisited URL, and mark it as visited
                dilemmaID = dilemma._id;

                res.redirect('/' + dilemma.id + '/' + dilemma.slug);

            })
        })

    })
dilemmaRouter.route('/:id/:slug')
    .get(function (req, res) {
        const _id = req.params.id;
        const _slug = req.params.slug;

        let query = {
            id: _id,
            slug: _slug
        }

        Dilemma.findOne(query, function (err, dilemma) {
            if (err) {
                console.log(err);
            } else {
                if (dilemma === null) {
                    res.redirect('/');
                } else {
                    res.render('index', {
                        dilemma: dilemma
                    })
                }


            }
        })
    })
dilemmaRouter.route('/next')
    .get(function (req, res) {

        Dilemma.count().exec(function (err, count) {
           // find all documents 
            User.find({'idUserOrIP' : 'userIPorID'}).exec(function(user) {
                var userListSaw = user.listSaw;
            })
            // create a list with all your document id
            var allDocs = [1...100];
            // remove id already seen (user saw id 1 to 3)
            allDocs = [4...100];
            // random now store the index of the id of the document you want to display
            var random = Math.floor(Math.random() * allDocs.length);
             // just find your document with the id you just get
            Dilemma.find({'id' : allDocs[random]}).exec(function (err, dilemma) { //This function is supposed to redirect to an unvisited URL, and mark it as visited
                dilemmaID = dilemma._id;

                res.redirect('/' + dilemma.id + '/' + dilemma.slug);

            })
        })
})