【发布时间】:2016-06-18 21:33:21
【问题描述】:
显然这里有一些我不明白的地方。我执行Model.findOne() 查询,然后根据findOne() 结果执行Model.find() 查询。
Model.find() 查询中的文档 ID 看起来与我直接从 Mongo shell 查询相同文档时的格式不同。我已经谷歌了一个小时,但没有运气。我错过了什么??
Model.find() 结果...
{ _id: ObjectID { _bsontype: 'ObjectID', id: 'W_?àÅÜY¼-íbO' }, // <- Problem
salt: 'ULRtWgj0qgblPRbSPwTe4A==',
displayName: 'test tester',
provider: 'local',
username: 'tester',
created: Tue Jun 14 2016 06:36:48 GMT+0200 (Romance Daylight Time),
roles: [ 'user' ],
profileImageURL: 'modules/users/client/img/profile/default.png',
referrers: { level1: ObjectID { _bsontype: 'ObjectID', id: 'W^_ó 5IE\u0013
N\u0010c' } }, // <- Also problem
password: 'jXpp+RJIegHFKD50sL2aOlOUhE0rvYtSJZIDB244SDIw93yrbuFCTUJK3SZIs3F
w0DdHOUzlQoLQwTGEcF67Kg==',
email: 'test@example.com',
lastName: 'tester',
firstName: 'test',
__v: 0
}
Mongo Shell 结果
{
"_id" : ObjectId("575f89e0c5dc59bc2ded62d5"), // <- This is different!
"salt" : "ULRtWgj0qgblPRbSPwTe4A==",
"displayName" : "test tester",
"provider" : "local",
"username" : "tester",
"created" : ISODate("2016-06-14T04:36:48.130Z"),
"roles" : [
"user"
],
"profileImageURL" : "modules/users/client/img/profile/default.png",
"referrers" : {
"level1" : ObjectId("575ebef32035ccc8134e1063") // <- This too!
},
"password" : "jXpp+RJIegHFKD50sL2aOlOUhE0rvYtSJZIDB244SDIw93yrbuFCTUJK3S
ZIs3Fw0DdHOUzlQoLQwTGEcF67Kg==",
"email" : "test@example.com",
"lastName" : "tester",
"firstName" : "test",
"__v" : 0
}
>
谁能向我解释我缺少什么概念?我的大多数查找查询都很好......这是我第一次遇到这种情况。提前致谢。
【问题讨论】:
-
您使用的是哪个版本的 MongoDB 和 Mongoose?可能是版本不匹配(请参阅this post 及其一些答案)
-
好问题。从外观上看,MongoDB 版本是 3.0.5,Mongoose 版本是 4.4.8。根据此页面,这些版本应该兼容:mongoosejs.com/docs/compatibility.html
-
我经常使用非常相似的版本,从未遇到您描述的问题。可能值得尝试最新的 Mongoose (4.5.0),和/或完全重新安装所有依赖项(删除
./node_modules并运行npm install)以排除任何潜在问题。 -
我不确定我是否已经完全做到这一点 - 我在整个应用程序中的所有其他查询都可以正常工作。真正让我困惑的是,Mongo shell 只是有
ObjectId("xxxxx"),但 Mongoose 返回一个 json 对象,{ _bsontype: 'ObjectId', id: 'xxxxx' }。这正常吗?我不记得以前看到过这种差异。 -
我刚刚意识到我的其他函数也返回了类似 id 格式的结果,但它仍然能够正确匹配 id。不知道发生了什么,但这真的让我很困惑。