【问题标题】:Cannot applying find() method with Native MongoDB becaus of ID type由于 ID 类型,无法对 Native MongoDB 应用 find() 方法
【发布时间】:2014-06-27 23:36:21
【问题描述】:

我有一个获得结果所需的函数。

当我将 1 作为 _id 过滤器时,一切正常。

collectionPersonnel
     .find({ '_id' : 1 })
     .toArray(function (err, personnel) {
     console.log(personnel);  
  });

如果我给过滤器另一种方式,例如 user[0]['personnel_id'] - 即商店 1- 那么我只会得到 [] 结果;

collectionPersonnel
     .find({ '_id' : user[0]['personnel_id'] })
     .toArray(function (err, personnel) {
     console.log(personnel);  
  });

然后我尝试了另一种方法。但它不起作用,因为我使用了 string(user[0]['personnel_id']) 而不是 ObjectID。

var ObjectID = require('mongodb').ObjectID; 
var personnelPK_Hex = (user[0]['personnel_id']).toHexString(); 
var personnelPK = ObjectID.createFromHexString(personnelPK_Hex);   

我该怎么办?

编辑

我所有的代码都在下面;

module.exports = {

    show: function(req, res) {

        User.native(function(err, collectionUser) {
            if(err) {
                console.log("There is no exist a User by current_id");
            };
            collectionUser
            .find({'_id' : req.param('id')})
            .toArray(function (err, user) {

                Personnel.native(function(err, collectionPersonnel) {
                    if(err) {
                        // handle error getting mongo collection
                        console.log("There is no exist a Personel by current _id");
                    };
                    if(!collectionPersonnel) {
                        console.log("There is no exist a Personel by current _id");
                    };

                    // var ObjectID = require('mongodb').ObjectID; 
                    // var personnelPK_Hex = (user[0]['personnel_id']).toHexString(); 
                    // var personnelPK = ObjectID.createFromHexString(personnelPK_Hex);                  

                    collectionPersonnel
                    .find({ '_id' : user[0].personnel_id })
                    .toArray(function (err, personnel) {

                        console.log(personnel);   
                    });
                });
            });            
        });
    }
};

控制台的输出是; []

已解决

就像apsillers所说的那样。我错误地给了一个数字 _id 集合。 我已经修复了 _id 值,一切正常。

谢谢大家...

【问题讨论】:

  • 没错!你说的对!我不久前解决了问题。还是谢谢...
  • @apsillers - 继续发布您的解决方案作为答案,以便 OP 给您积分

标签: node.js mongodb sails.js mongodb-query node-mongodb-native


【解决方案1】:

user[0]['personnel_id'] 可能是一个字符串。对于 Mongo,"1"1 不同,这就是为什么您的文字数字 1 有效,但您的变量(包含字符串)无效。

请尝试使用一元加号将字符串转换为数字:+user[0]['personnel_id']

【讨论】:

    【解决方案2】:

    尝试使用 user[0].personal_id 而不是 user[0]['personnel_id'] 请提供您的架构设计,以便更好地弄清楚你到底错过了什么。

    我试过这样

    collectionPersonnel
     .find({ '_id' : user[0].personnel_id })
     .toArray(function (err, personnel) {
     console.log(personnel);  
      });
    

    【讨论】:

    • 感谢您的建议,大卫。我已经尝试过你的建议,但它对我不起作用。同时,当我将 user[0].personnel_id 或 user[0]['personnel_id'] 写入 console.log 时,控制台会正确输出 1。 (我不知道如何提供架构设计)我一直在尝试嵌套查询。首先,查询“用户”集合,其次使用返回我第一个查询结果的“User.personnel_id”查询人员集合。实际上这是一个嵌套查询。我正在编辑我的问题。再次感谢您。
    猜你喜欢
    • 2018-08-26
    • 2021-10-12
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多