我有诗集。集合中的文档具有以下结构:
{
"_id" : "Romeo and Juliet",
"acts" : [
{
"title" : "ACT I",
"scenes" : [
{
"title" : "SCENE I. Verona. A public place.",
"action" : [
{
"character" : "SAMPSON",
"says" : [
"Gregory, o' my word, we'll not carry coals."
]
},
{
"character" : "GREGORY",
"says" : [
"No, for then we should be colliers."
]
},
// ...
{
"character" : "GREGORY",
"says" : [
"To move is to stir; and to be valiant is to stand:",
"therefore, if thou art moved, thou runn'st away."
]
},
{
"character" : "SAMPSON",
"says" : [
"A dog of that house shall move me to stand: I will",
"take the wall of any man or maid of Montague's."
]
},
{
"character" : "GREGORY",
"says" : [
"That shows thee a weak slave; for the weakest goes",
"to the wall."
]
}
// ...
]
}
// ...
]
}
// ...
]}
我想计算每首诗的行为和场景数量。 我试图做这样的事情,但结果是不正确的。
db.poems.aggregate([{$unwind:"$acts"}, {$unwind:"$acts.scenes"}, {$group: {_id: "$_id", pa: {$push: {poemAct:"$acts"}}, ps: {$push: {poemScenes:"$acts.scenes"}}}}, {$project: {numberOfActs: {$size: "$pa"}, numberOfScenes: {$size: "$ps"}}}]).pretty()
请帮助某人:D
答案 0 :(得分:3)
您可以尝试以下聚合。此处不需要.parent{
width:250px;
height:250px;
background-color:green;
position:relative;
}
.child{
display:flex;
flex-direction:column;
background-color:red;
position:absolute;
}
.box_container{
display:flex;
flex-direction:row;
align-items:center;
}
.intro{
position:relative;
top:10px
}
.box{
width:15px;
height:15px;
background-color:yellow;
}
。
$map
和$size
输出具有场景大小和$sum
的数组以计算数组值。
$unwind
答案 1 :(得分:1)
db.poems.find().forEach(function(poem) {
var res = {};
res._id = poem._id,
res.acts = poem.acts.length;
res.scenes = poem.acts.scenes.length;
printjson(res)
})