【发布时间】: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