【问题标题】:Blank result from mongodb find query来自 mongodb 查找查询的空白结果
【发布时间】:2020-02-07 14:34:22
【问题描述】:

我在 mongo shell 中运行以下查询并获得所需的结果。

db.messagelist.find({userID : ObjectId("5e39a9c107357487435d5849")});

输出

{ "_id" : ObjectId("5e39a9c1073574eb1a5d584a"), "message" : "I am at some place", "messageType" : "0", "messageLocation" : "0", "userID" : ObjectId("5e39a9c107357487435d5849"), "timeStamp" : "'1580837313163'" }

但是当我为它创建如下路由时,它返回空白结果:

router.get('/messagelist/:id', function (req, res) {
    var db = req.db;
    var collection = db.get('messagelist');
    var id = req.params.id;
    //console.log("id is: "+id);
    //collection.find({userID : 'ObjectId("'+id+'")'});
    collection.find({"userID" : id}, {}, function (e, docs) {      
        docs = JSON.stringify(docs);
        console.log("docs: "+docs); //empty
        res.send(docs);         
    });
});

语法有问题吗?!请有任何建议。

编辑:

此查询也有效。所以在这种情况下语法是正确的。我不知道用户 ID 有什么问题。

router.get('/messagelist/:id', function (req, res) {
    var db = req.db;
    var collection = db.get('messagelist');
    var id = req.params.id;
    //console.log("id is: "+id);
    //db.messagelist.find({userID : 'ObjectId("'+id+'")'});

    collection.findOne({_id : id}, function(e, docs) {      
        console.log("docs: "+docs);
        docs = JSON.stringify(docs);
        console.log("docs: "+docs);
        res.send(docs);
    });
}); 

除了 userID 的情况外,所有参数都有效。真奇怪。它甚至不是关键字。

【问题讨论】:

    标签: javascript node.js mongoose mongodb-query


    【解决方案1】:

    尝试使用 findOne 方法,因为它返回单个对象/任意文档。如果您通过 _id(文件 id)查询,请改用 findById()。

    https://mongoosejs.com/docs/api.html#model_Model.findOne

    【讨论】:

    • Niether 正在这条路线上工作。我得到空白结果。 find 查询在 mongodb 终端中正确获取条件和输出。 findOne() 也适用于终端。
    【解决方案2】:
    Mongodb 中的

    find 方法意味着 查找所有文档,因此您无需传递特定属性即可查找所需内容。相反,使用 findOne 并传递属性或 findById 方法并传递 id。

    【讨论】:

    • Niether 正在这条路线上工作。我得到空白结果。 find 查询在 mongodb 终端中正确获取条件和输出。 findOne() 也适用于终端。
    【解决方案3】:

    试试

    collection.find(id).then(res=>console.log(res)).catch(err=>console.log(err));
    

    【讨论】:

      【解决方案4】:
      router.get('/messagelist/:id', function (req, res) {
          var db = req.db;
          var collection = db.get('messagelist');
          var id = req.params.id;
          //collection.find({userID : 'ObjectId("'+id+'")'});
          collection.find({"userID" : id}, {}, function (e, docs) {      
              docs = JSON.stringify(docs);
              res.send(docs);         
          });
      });
      

      问题是userID和id(从参数接收)的表示不一样。

      userID 格式为:ObjectId("5e3c13d7b5d89bc92c932243")

      id 格式为:"5e3c13d7b5d89bc92c930000"

      这样做,'ObjectId("'+id+'")' 会导致:"ObjectId(\"5e3c13d7b5d89bc92c930000\")"

      所以基本上,userID 无法匹配 id'ObjectId("'+id+'")'

      解决方案:
      由于其他依赖关系,我无法更改 id 存储在任何集合中的格式。 所以我在 messagelist-collection 中创建了一个单独的 referenceID(以所需的格式)来存储来自其他集合的相应文档的 objectId。

      router.get('/messagelist/:id', function (req, res) {
          var db = req.db;
          var collection = db.get('messagelist');
          var id = req.params.id;
          collection.findOne({refID: "'"+id+"'"}, {}, function(e, docs) {      
              docs = JSON.stringify(docs);
              res.send(docs);
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 2019-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 2021-10-26
        相关资源
        最近更新 更多