【问题标题】:Returned Mongoose id different format than MongoDB shell results?返回的 Mongoose id 格式与 MongoDB shell 结果不同?
【发布时间】: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。不知道发生了什么,但这真的让我很困惑。

标签: node.js mongodb mongoose


【解决方案1】:

好的,这听起来真的很愚蠢,但是在我的Mongoose.find() 结果中,我没有使用results.idresults._id 之类的东西获取id,而是将它与results._id.id 匹配的奇怪格式的id (因为_id 本身就是一个带有 id 值的对象)。

对我来说似乎很老套,但它确实有效。所以现在我有类似的东西:

id = results._id.id || results.id;

看起来很奇怪,但确实有效。

【讨论】:

  • 作为最后的尝试来了解这个问题(著名的遗言......),你可以试试这个:mongoexport -d DB -c COLLECTION --limit 10。这应该以 JSON 格式导出您收藏的 10 条记录。在我的例子中,ObjectId 看起来像这样:"_id":{"$oid":"..."}。我想知道您的数据库是否也一样。
  • 您也可以通过在返回的结果上调用.toObject() 来获得良好的结果。
  • @robertklep 这是该用户的结果:{"_id":{"$oid":"575f89e0c5dc59bc2ded62d5"},"salt":"ULRtWgj0qgblPRbSPwTe4A==","di splayName":"test tester","provider":"local","username":"tester","created":{"$dat e":"2016-06-14T04:36:48.130Z"},"roles":["user"],"profileImageURL":"modules/users /client/img/profile/default.png","referrers":{"level1":{"$oid":"575ebef32035ccc8 134e1063"}},"password":"jXpp+RJIegHFKD50sL2aOlOUhE0rvYtSJZIDB244SDIw93yrbuFCTUJK 3SZIs3Fw0DdHOUzlQoLQwTGEcF67Kg==","email":"test@example.com","lastName":"test er","firstName":"test","__v":0}
  • @slugonamission 感谢您的建议 - 这是个好主意,但不幸的是,对我的情况没有帮助。
  • @aikorei 是的,这看起来与我得到的相似,所以我正式没有想法:(
猜你喜欢
  • 2014-05-05
  • 2017-10-05
  • 1970-01-01
  • 2021-07-22
  • 2020-02-15
  • 2023-03-09
  • 1970-01-01
  • 2016-11-18
  • 2021-08-13
相关资源
最近更新 更多