在loopback和mongo中同样的“find”查询在不同的服务器上有不同的行为

我想在一个回环模型JS文件中进行查询。 它非常简单,如下所示:

//isAdmin function: gets a boolean if a given userProfileId is an admin or not. Userprofile.isAdmin = function(userProfileId, cb) { let role = app.models.Role; let rolemapping = app.models.RoleMapping; let filter = { "where": { "name": "admin" }, "fields": ["id"] }; role.findOne(filter, function(err, instance) { let filter2 = { "where": { "roleId": instance.id , "principalId": userProfileId, }, "fields": ["id"] }; rolemapping.find(filter2, function(err, instance) { if (instance != "") { cb(null, true); } else { cb(null, false); } }); }); }; 

这在我们的开发服务器中是完美的。 该服务器中的filter2console.log返回:

 { where: { roleId: 5890ef8bbef9b73e568c6933, principalId: '5890ef8bbef9b73e568c6932' }, fields: [ 'id' ] } } 

instance.idconsole.log如下所示:

 ObjectID { _bsontype: 'ObjectID', id: 'Xï¾ù·>Vi3 } 

问题出在我们的生产服务器上。 我做了部署过程,结果稍有不同,但它根本不工作:(。

filter2console.log返回:

 { where: { roleId: 58921dff16d9a37009e21104, principalId: '5890ef8bbef9b73e568c6932' }, fields: [ 'id' ] } } 

而且, console.log(instance.id)返回:

 ObjectID { _bsontype: 'ObjectID', id: Buffer [ 88, 146, 29, 255, 22, 217, 163, 112, 9, 226, 17, 4 ] } 

即使在数据库中有满足查询的文档,我们生产服务器中的这个查询也不会返回任何文档。

我已经比较了所有的npm包,都是完全相同的版本。 以及服务器(我们正在使用Debian 8)和mongo(v3.2.10)。

有没有想法来解决这个问题?

提前致谢!

佩德罗。

使用相关模型的ID进行查询时遇到过类似的问题。 通过select类似而不是像下面这样的等价来解决问题:

 {where: {and:[{roleId: {like:'.*'+roleIdVar+'*.', options:'i'}}, {principalId: {like:'.*'+principalIdVar+'*.', options:'i'}}] } }, {fields: [ 'id' ] } 

还请注意,字段投影应该是与wherefilter不同的对象。

你有没有在“userProfile”.json文件中检查userProfile idtypes? 这似乎是一个types的问题。 你可以解决它在你的model.json文件中添加idtypesstring的属性,如下所示:

 "properties": { "userProfileId": { "type": "string" }