我刚刚开始学习mongodb,所以我对查询的选择目前还不是很好。 所以我会直接解决这个问题。以下是我的每个用户的文档
{
id:"14198959",
user_name:"kikStart2X"
friends:[
{
friend_id:"1419897878",
friend_name:"nitpick",
profile_picture:"some image data",
},
{
friend_id:"14198848418",
friend_name:"applePie",
profile_picture:"some image data",
}, //etc
],
games:[
{
game_id:"1" ,
game_name:"Bunny Hop"
},
{
game_id:"2" ,
game_name:"Racing cars",
},
],
}
现在集合中的所有文档都具有相同的结构
1)friends数组代表我朋友的用户
2)games array代表我玩过的游戏
我的朋友将拥有与包含他们所玩游戏的游戏阵列相同的文档结构
我想要的是以升序/降序或任何顺序列出我和朋友之间最常见的游戏。
结果应如下所示
{
result:
[
{
game_id:"1" ,
game_name:"Bunny Hop",
friends:
[
{
friend_id:"1419897878",
friend_name:"nitpick",
profile_picture:"some image data",
},
{
friend_id:"14198848418",
friend_name:"applePie",
profile_picture:"some image data",
},
]
},
{
game_id:"2" ,
game_name:"Racing cars",
friends:
[
{
friend_id:"71615343",
friend_name:"samuel",
profile_picture:"some image data",
},
]
}
]
}
我知道这有点难以实现,但我不知道该怎么做,并在互联网上搜索了几个小时。 在此先感谢所有MongoDB冠军。
答案 0 :(得分:1)
您可以尝试以下聚合查询。
对于每个朋友游戏,查询将$unwind
friends
数组跟进$lookup
。
下一步是$unwind
friendsgames
,然后使用$setIntersection
阶段中的$project
进行比较,找到输入文档games
与每个friendsgames
之间的常见游戏{1}}。
最后一步是$group
games
用friends
收集相同游戏的db.collection.aggregate( [
{ $unwind:"$friends" },
{
$lookup: {
from: collectionname,
localField: "friends.friend_id",
foreignField: "id",
as: "friendsgames"
}
},
{ $unwind:"$friendsgames" },
{ $project:{commongames:{$setIntersection:["$games", "$friendsgames.games"]}, friends:1 }},
{ $unwind:"$commongames" },
{ $group:{_id:"$commongames", friends:{$push:"$friends"} } }
] )
。
document.addEventListener("DOMContentLoaded", function(event) {
makingRanking();
});
function makingRanking(){
var ranking_groups = document.getElementsByClassName("Ranking");
for (var i=0; i < ranking_groups.length; i++){
var ranking_items = ranking_groups[i].getElementsByClassName("DemoQuest");
for(var j=0; j < ranking_items.length; j++){
var id = ranking_items[j].id;
ranking_items[j].draggable = true;
ranking_items[j].ondragstart = function(){ dragStart(event);}
ranking_items[j].ondrop = function(){ dropHappened(event);}
ranking_items[j].ondragover = function(){ dragAndDrop(event);}
}
}
};
function dragStart(ev){
if (ev.target.classList.contains("DemoQuest")){
ev.dataTransfer.setData("text", ev.target.id);
console.log("Dragging: " + ev.target.children[0].innerHTML);
} else {
ev.dataTransfer.setData("text", ev.target.parentElement.id);
console.log("Dragging: " + ev.target.innerHTML);
}
};
function dropHappened(ev){
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
var dragged_item = document.getElementById(data);
var temp = "";
console.log("Dropping onto: " + ev.target.children[0].innerHTML);
if (data != ""){
if (ev.target.classList.contains("DemoQuest")){
temp = ev.target.outerHTML;
ev.target.outerHTML = document.getElementById(data).outerHTML;
ev.target.draggable = true;
ev.target.ondragstart = function(){ dragStart(event);}
ev.target.ondrop = function(){ dropHappened(event);}
ev.target.ondragover = function(){ dragAndDrop(event);}
} else {
temp = ev.target.parentElement.outerHTML;
ev.target.parentElement.outerHTML = document.getElementById(data).outerHTML;
ev.target.parentElement.draggable = true;
ev.target.parentElement.ondragstart = function(){ dragStart(event);}
ev.target.parentElement.ondrop = function(){ dropHappened(event);}
ev.target.parentElement.ondragover = function(){ dragAndDrop(event);}
}
document.getElementById(data).id = data + '_temp';
document.getElementById(data).outerHTML = temp;
document.getElementById(data + '_temp').id = data;
document.getElementById(data).draggable = true;
document.getElementById(data).ondragstart = function(){ dragStart(event);}
document.getElementById(data).ondrop = function(){ dropHappened(event);}
document.getElementById(data).ondragover = function(){ dragAndDrop(event);}
} else {
console.log("HOW THE FUCK?");
console.log("Dropping on to: " + ev.target.id);
}
};
function dragAndDrop(ev){
ev.preventDefault();
//console.log("Passing over: " + ev.target.children[0].innerHTML);
};