两个独立的 MongoDB 集合将结果组合起来进行循环,没有任何关系 [英] Two separate MongoDB collections combine the results to loop through with no relationship
本文介绍了两个独立的 MongoDB 集合将结果组合起来进行循环,没有任何关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个包含投票和评论的活动源.两个集合之间没有关系.
I am trying to create an activity feed with votes and comments. There is no relationship between the two collections.
获取评论:
$CommentsCollection = $this->db->comments;
$options = ['sort' => ['created' => -1], 'limit' => 15];
$data = array ();
$resultComments = $CommentsCollection->find($data, $options);
获得选票:
$VotesCollection = $this->db->votes;
$options = ['sort' => ['created' => -1], 'limit' => 15];
$data = array ();
$resultVotes = $VotesCollection->find($data, $options);
现在我如何组合这两个独立的、不相关的集合结果并按创建(日期)排序.
Now how do I combine these two separate, non related collection results and order them by created (date).
我不认为聚合或查找是正确的方法,因为这两个表不相关.我只想组合结果并按日期对其进行排序,以便我可以循环浏览并将它们显示在活动提要上.
I don't think aggregation or lookup is the right method because these two tables are not related. I just want to combine the results and order them by date so I can loop through and display them on the activity feed.
推荐答案
我所做的是使用:
$finalArray = array_merge($resultComments, $resultVotes);
合并结果.然后按日期排序:
to merge the results. Then to sort by date did:
usort($finalArray, function($a, $b) {
return strtotime($a['created']) - strtotime($b['created']);
});
我想太多了.
这篇关于两个独立的 MongoDB 集合将结果组合起来进行循环,没有任何关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文