【问题标题】:Return document with all array of objects where element exists返回包含元素存在的所有对象数组的文档
【发布时间】:2019-05-10 13:54:15
【问题描述】:

我正在尝试返回一个包含userAnswer 存在的对象数组的文档。我下面的查询只返回数组的第一个对象。我在这里想念什么?它应该返回两个。

正在使用 Meteor 和 MongoDB。

MongoDB:document

{
  _id: 1,
  questions: [
    { question: 'test question', userAnswer: 'answer' },
    { question: 'test question two', userAnswer: 'answertwo' },
    { question: 'test question three' }
  ]
};

MongoDB:Query

ConductedExams.findOne(
  {
    userId,
    examId,
    userCompletedExam: null
  },
  { fields: { questions: { $elemMatch: { userAnswer: { $exists: true } } } } }
);

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    "findOne" 只返回一个元素。 您应该使用“查找”。

    【讨论】:

    • 加上 fetch 如果它是从 Meteor 方法返回或在其他地方使用的。否则它返回一个光标。然而,在出版物中,重要的是返回光标而不是获取的文档。
    • 当然。类似于: CollectionName.find({ stuff: something }).fetch();
    猜你喜欢
    • 2021-10-02
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多