这是我的例子。
items: [
{
mart_code : 1,
director: "James",
category: "fruit",
name: "apple",
money: 5000
},
{
mart_code: 1
director: "James",
category: "toy",
name: "dragon",
money: 15000
},
{
mart_code: 2,
director: "Sam",
category: "fruit",
name: "orange",
money: 3500
}
]
我想得到结果。
martList: [ {
mart_code: 1,
directorList:[
{
director: "James",
money: 20000
}
],
categoryList: [
{
category: "fruit",
money: 5000
},
{
category: "toy",
money: 15000
}]
},
{
mart_code: 2,
directorList:[
{
director: "Sam",
money: 3500
}
],
categoryList: [
{
category: "fruit",
money: 3500
}]
}
]
我试图$ facet和$ group,$ unwind $ project ... 有没有有效的方法?
答案 0 :(得分:1)
希望 $ group 和 $ project 是获得结果的有效方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="BotChat/botchat.css" rel="stylesheet" />
<script src="BotChat/botchat.js"></script>
<style>
#mychat {
margin: 10px;
position: fixed;
bottom: 30px;
right: 10px;
z-index: 1000000;
}
</style>
</head>
<body>
<div id="container">
<h1>Hello World</h1>
<!--other page contents-->
<img id="mychat" src="https://i.stack.imgur.com/RD7i4.png" style="float:right" />
</div>
</body>
</html>
<script>
(function () {
var div = document.createElement("div");
document.getElementsByTagName('body')[0].appendChild(div);
div.outerHTML = "<div id='botDiv' style='width: 400px; height: 0px; margin:10px; position: fixed; bottom: 0; right:0; z-index: 1000;><div id='botTitleBar' style='height: 40px; width: 400px; position:fixed; cursor: pointer;'></div></div>";
BotChat.App({
directLine: { secret: '{directline_secret}' },
user: { id: 'You' },
bot: { id: '{your_botid}' }
}, document.getElementById("botDiv"));
document.getElementsByClassName("wc-header")[0].setAttribute("id", "chatbotheader");
document.querySelector('body').addEventListener('click', function (e) {
e.target.matches = e.target.matches || e.target.msMatchesSelector;
if (e.target.matches('#chatbotheader')) {
var botDiv = document.querySelector('#botDiv');
botDiv.style.height = "0px";
document.getElementById("mychat").style.display = "block";
};
});
document.getElementById("mychat").addEventListener("click", function (e) {
document.getElementById("botDiv").style.height = '500px';
e.target.style.display = "none";
})
}());
</script>
答案 1 :(得分:0)
我尝试过这个。
db.collection.aggregate([
{$unwind : "$items" },
{$group : {
_id : {
mart_code : '$mart_code',
director : '$director'
},
money: {$sum : '$money'},
entry: {$push: {$$ROOT}}
}},
{$group : {
_id : '$_id.mart_code',
directorList : {$push : {
director: "$_id.director",
money: '$money'
}},
entry: {$push: "$entry"}
}},
{ $unwind : "$entry" },
{ $unwind : "$entry" },
{$group : {
_id : {
mart_code : '$_id',
directorList : '$directorList',
category : '$entry.category'
},
money: {$sum: "$entry.money"}
}},
{$group: {
_id: {
mart_code : '$_id',
directorList : '$directorList',
},
categoryList:{ $push:{
category: "$_id.category",
money: "$money"
}}
}}
{$project : {
_id : 0,
categoryList: "$categoryList",
directorList: "$_id.directoryList"
}}
])
这是没有效率的方法.....