【问题标题】:Mongoosejs .find returning whole model instead of document [duplicate]Mongoosejs .find 返回整个模型而不是文档 [重复]
【发布时间】:2020-04-15 22:09:36
【问题描述】:

我正在对现有模型进行 .find() 查询。我过去使用过这段代码并且没有改变任何东西,但现在突然由于某种原因它无法正常工作。我在想 MongoDB 或 MongooseJS 更新并且功能已经改变。

var retrieve = function() {
  Repo.find({}, function(err, docs) {
    console.log(docs)
  })
};

retrieve();

返回

[
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: {},
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5e02e91c908f0f086e737189,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      __v: 0,
      stars: 2,
      id: 1322,
      url: 'url',
      name: 'name',
      _id: 5e02e91c908f0f086e737189
    },
    '$init': true
  },
  model {
    '$__': InternalCache {
      strictMode: true,
      selected: {},
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      version: undefined,
      getters: {},
      _id: 5e02e92c3f6b72088246c563,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: {},
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': true
    },
    isNew: false,
    errors: undefined,
    _doc: {
      __v: 0,
      stars: 2,
      id: 2,
      url: 'url1',
      name: 'name1',
      _id: 5e02e92c3f6b72088246c563
    },
    '$init': true
  }
]

它应该返回

[{name: 'name', id: 2, url: 'url', stars: 2},
{name: 'name1', id: 1322, url: 'url1', stars: 2}]

我不知道为什么会这样

---- 为 Ahsok 编辑 --- 我尝试使用您的代码

const retrieve = () => {
  Repo.find({})
  .then(repo => {
    console.log({ repo })
  })
  .catch(error => {
    console.log({ error })
  })
};

它仍然没有返回它需要的东西。现在它回来了

{
  repo: [
    model {
      '$__': [InternalCache],
      isNew: false,
      errors: undefined,
      _doc: [Object],
      '$init': true
    },
    model {
      '$__': [InternalCache],
      isNew: false,
      errors: undefined,
      _doc: [Object],
      '$init': true
    }
  ]
}

与上面返回的内容相同,只是格式略有不同

【问题讨论】:

  • 你可以拍reference
  • @Ashok 那是引用 collection.find()。我正在尝试执行 model.find()
  • 有人知道为什么会这样吗?我遇到了同样的问题。但之前效果很好
  • 如果以下任何解决方案对任何人都不起作用,那么我的问题是我的模型没有在 mongoose.connect() 中定义

标签: javascript node.js database mongodb mongoose


【解决方案1】:

我想通了。原来我必须将节点恢复到以前的版本才能成为默认行为。正如 Mohammed 指出的那样使用 .lean() 也可以,但我想知道为什么我的代码的行为与以前不同,结果证明这是由节点更新引起的。

【讨论】:

    【解决方案2】:

    这是预期的行为,Mongoose find 查询总是返回一个 mongoose 的实例,即你得到的。有两种处理方法:

    1. 自行将您的响应转换为普通对象:

    console.log(docs.toObject())

    1. 让猫鼬自己为你做这件事(使用 lean):
    Repo.find({}).lean().exec(function(err, docs) {
        console.log(docs);
    });
    

    您可以阅读有关精益的更多信息here

    希望这会有所帮助:)

    【讨论】:

    • .lean() 应该会有所帮助。但在这种情况下,我们不能使用 .save() 方法和其他此类方法。
    • 是的,这是正确的,这就是您可以选择其他选项的地方,例如。使用您的实际实例来更新/保存文档并将普通对象作为单独的变量进行播放:)
    【解决方案3】:

    如果您使用的是异步函数,请使用此语法

    const retrieve = async () => {
      const repo = await Repo.find({})
      console.log(repo)
    };
    

    如果您不知道上面发生了什么,请使用此语法

    const retrieve = () => {
      Repo.find({})
      .then(repo => {
        console.log({ repo })
      })
      .catch(error => {
        console.log({ error })
      })
    };
    

    您也可以从here 乘车和乘车。

    为什么会发生这种情况因为找到返回游标或承诺从中检索_doc,您需要使用承诺。 在这里第一种类型的解决方案很流行清理代码。

    【讨论】:

    • 这不起作用,我已经编辑了我的原始帖子以显示使用您的代码时会发生什么
    猜你喜欢
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2020-09-12
    • 2023-01-17
    • 1970-01-01
    • 2016-12-18
    相关资源
    最近更新 更多