找到&从数组返回第一个匹配的子文档(Meteor / Mongo)

时间:2015-03-20 10:24:21

标签: arrays mongodb meteor subdocument

如何查找并返回'任务中的第一个子文档'匹配完成的数组:true?

使用findOne返回整个文档..还有另一个返回子文档的函数吗?

{
  title: 'awebsite.com'
  company: 'a company'
  companyID: Random.id()
  category: 'website'
  starred: false
  timeline: {
    tasks: [
      {
        name: 'task1'
        completed: true
        todos: [
          {todo: 'something', completed: false, todoID: Random.id()}
          {todo: 'something', completed: false, todoID: Random.id()}
          {todo: 'something', completed: false, todoID: Random.id()}
        ]
      }
      {
        name: 'task2'
        completed: false
        todos: [
          {todo: 'something', completed: false, todoID: Random.id()}
          {todo: 'something', completed: false, todoID: Random.id()}
          {todo: 'something', completed: false, todoID: Random.id()}
        ]
      }
    ]
  }
}

2 个答案:

答案 0 :(得分:1)

Meteor用户:在撰写本文时(版本1.0.4.1),不支持客户端上的聚合。

我想将代码保存在客户端上,并且还想要反应性,所以这是我的解决方案:

db.projects

// simplified structure

{
      title: 'awebsite.com'
      company: 'a company'
      companyID: Random.id()
      category: 'website'
      starred: false
      tasks: [
          {
            completed: true
            name: 'task1'
            category: 'ideas'
            todos: [
              {todo: 'something', completed: false, todoID: Random.id()}
              {todo: 'something', completed: false, todoID: Random.id()}
              {todo: 'something', completed: false, todoID: Random.id()}
            ]
          }
          {
            completed: false
            name: 'task2'
            category: 'ideas'
            todos: [
              {todo: 'something', completed: false, todoID: Random.id()}
              {todo: 'something', completed: false, todoID: Random.id()}
              {todo: 'something', completed: false, todoID: Random.id()}
            ]
          }
        ]
    }

../ projects.coffee

Meteor.subscribe 'projects'
Tasks = new (Mongo.Collection)(null)   //use (null) to create client-only collection

Template.projects.rendered = ->
  results = Projects.findOne { title: 'awebsite.com' },
    fields: tasks: 1

  _.each results.tasks, (task) ->
    Tasks.insert (task)

Template.projects.helpers
  currentTask: ->
    Tasks.findOne completed: false

答案 1 :(得分:0)

您可以通过 aggregation 执行此操作,您可以利用索引并使用$match管道进行限制。使用 $unwind 运算符将您的任务数组解构为可以匹配的文档流。因为您只想返回“'tasks'数组中匹配已完成的第一个子文档:true ”,您可以使用 $limit 运算符最后一个管道阶段只返回一个子文档:

db.collection.aggregate([
  { 
      $match: {
          "timeline.tasks.completed": true
      }
  },
  { 
      $unwind: "$timeline.tasks" 
  },
  { 
      $match: {
          "timeline.tasks.completed": true
      }
  },
  { 
      $group: {
           _id: {
                "tasks": "$timeline.tasks"
           }
      }
  },
  {
      $project: {
          _id: 0,
          tasks: "$_id.tasks"
      }
  },
  {
      $limit: 1
  }
])

结果:

{
    "result" : [ 
        {
            "tasks" : {
                "name" : "task1",
                "completed" : true,
                "todos" : [ 
                    {
                        "todo" : "something",
                        "completed" : false,
                        "todoID" : "jfoe84jn"
                    }, 
                    {
                        "todo" : "something",
                        "completed" : false,
                        "todoID" : "yr934hjs"
                    }, 
                    {
                        "todo" : "something",
                        "completed" : false,
                        "todoID" : "84hdkl0t"
                    }
                ]
            }
        }
    ],
    "ok" : 1
}