无法使用node.js在mongodb集合中删除

时间:2019-01-30 13:17:46

标签: node.js mongodb

iam使用mongodb将express-session存储为express-session docs,它可以很好地与插入和更新一起工作,但是无法从集合中删除会话,我不知道为什么,但是在我重新启动服务器后第一次起作用

async function logout(req, res) {

if (!req.sessionID || !req.session.adminId){
    res.send({status:400,detail:"Invalid Request"})
    return;
}
if (sessionStore!=null){
    sessionStore.remove({"_id":req.sessionID},true)'
    res.send({status:200,detail:"Logged Out"})
}
else {res.send({status:400,detail:"Something went wrong"})
 }
}

/*sessionStore here is my collection object e.g db.collection('sessions') as below*/
mongoClient.connect("mongodb://localhost:27017/AdminSessionDb", function(err, db) {
    if (err) {
        console.log('Error connecting to MongoDB-AdminSessionDB, err);
        return;
    }
    else {
        // console.log('Connected to MongoDB-capptinAdminSessions => auth.js');
        adminSessionsCollection = db.collection("sessions");
    }
});

2 个答案:

答案 0 :(得分:2)

尝试将id包裹在ObectId()中,如下所示:db.collection.remove({“ _id”:ObjectId(“ ID”)});

async function logout(req, res) {

if (!req.sessionID || !req.session.adminId){
    res.send({status:400,detail:"Invalid Request"})
    return;
}
if (sessionStore!=null){
    sessionStore.remove({"_id": Mongoose.Types.ObjectId(req.sessionID)})
    res.send({status:200,detail:"Logged Out"})
}
else {res.send({status:400,detail:"Something went wrong"})
 }
}

/*sessionStore here is my collection object e.g db.collection('sessions') as below*/
mongoClient.connect("mongodb://localhost:27017/AdminSessionDb", function(err, db) {
    if (err) {
        console.log('Error connecting to MongoDB-AdminSessionDB, err);
        return;
    }
    else {
        // console.log('Connected to MongoDB-capptinAdminSessions => auth.js');
        adminSessionsCollection = db.collection("sessions");
    }
});

答案 1 :(得分:0)

所以最后我找到了一个解决方案,我不知道为什么,但是它不会基于_id删除,而且我的id是字母数字,例如“ 2SXBWzAMq6laXsmYtLGqJHpdt3omDesG”,它没有在node.js中删除,所以我尝试了一个技巧具有该行/记录的数字唯一ID字段,并将此uniqueId附加到会话属性以及

sessionStore.deleteOne({
        uniqueId: parseInt(req.session.uniqueId),
    })

并通过

这样的uniqueId删除记录
            <!DOCTYPE html>
            <html lang="en">
            <head>
              <meta charset="UTF-8">
              <title></title>
            </head>
            <body>
                <div id="div4">
                  <img class="bottom" src="dcim/20190202_1024322.jpg" />
                  <img class="top" src="dcim/20190202_102432.jpg" />
                  <audio id="audio4">
                    <source src="div4.mp3"></source>
                  </audio>
              </div>
              <script>
                var div4 = document.querySelector('#div4') 
                var audio = document.querySelector('#audio4')
                div4.addEventListener("mouseenter", function(){
                  audio.play()
                });
                div4.addEventListener("mouseout", function(){
                  audio.pause()
                });
            </script>
            </body>
            </html>

现在删除该行或记录。