这是我试图测试的Mongo系列:
{"_id":{"$id":"54d5002adc533bf41000002c"},"tasks":[{"taskID":1,"taskName":"Task 1 Name Here","subTasks":[1],"coords":{"gantt":{"x":10,"y":30},"pert":{"x":90,"y":100}}},{"taskID":2,"taskName":"Task 2 Name Here","participators":[1,2],"startDate":"5-12-2014","endDate":"5-21-2014"},{"taskID":3,"taskName":"Task 3 Name Here","subTasks":[3],"participators":[1]}],"participators":[{"participatorID":1,"participatorName":"Participator 1 Name Here"},{"participatorID":2,"participatorName":"Participator 2 Name Here"}]}
我正在尝试根据ID过滤此数据,然后只返回一组任务,使用taskID进行过滤。
以下是我正在使用的代码:
$cursor = $this->mongo->findOne(['_id' => $mongoID, 'tasks.taskID' => 2], ['_id' => false, 'tasks.taskID' => true, 'tasks.coords.gantt' => true]);
这应该只返回taskID 2的数据;你可以看到我试图过滤到只显示2的taskID。但它会返回:
{"tasks":[{"taskID":1,"coords":{"gantt":{"x":10,"y":30}}},{"taskID":2},{"taskID":3}]}
我不知道我是如何过滤结果的,所以只返回指定的taskID数据。
谢谢。
- 更新 - 使用以下代码修复:
$cursor = $this->mongo->findOne(['_id' => $mongoID], ['_id' => false, 'tasks' => ['$elemMatch' => ['taskID' => 1]], 'tasks.coords.gantt' => true]);