下面的mongodb数据有问题。
我想获取数据[projects][log][subject]
。
所以,我试过这个
$project':{_id:0, projects.log.subject:1}
但语法不正确..
{
"_id": ObjectID("569f3a3e9d2540764d8bde59"),
"A": "book",
"server": "us",
"projects": [
{
"domainArray": [
{
~~~~
}
],
"log": [
{
~~~~~,
"subject": "I WANT THIS"
}
],
"before": "234234234"
},
{
"domainArray": [
{
~~~~
}
],
"log": [
{
~~~~~,
"subject": "I WANT THIS"
}
],
"before": "234234234"
},....
] //end of projects
}//end of document
如何通过[subject]
获取数据分组?我不知道这个......
Edited- 我期待这样的数据
{
"subject":"first",
"subject":"second",
"subject":"third",
"subject":"~~~"
}
有可能吗?我只想获得一系列主题。
答案 0 :(得分:0)
不确定这是否是您的预期结果。你可以试试这个:
db.project.aggregate([
{$project:{projects:1,_id:0}},
{$unwind:"$projects"},
{$unwind:"$projects.log"},
{$project:{subject:"$projects.log.subject",_id:0}}
])
对于上面的字数结果,和Map-Reduce如下:
//map function
var map = function() {
for(var i in this.projects)
{
for(var j in this.projects[i].log)
{
var arrayWords = this.projects[i].log[j].subject.split(" ");
for(var k = 0; k < arrayWords.length; k++)
{
emit(arrayWords[k],{occurance:1});
}
}
}
};
//reduce function
function reduce(word, arrayOccurance) {
var totalOccurance = 0;
for (var i = 0; i < arrayOccurance.length; i++)
{
totalOccurance = totalOccurance + arrayOccurance[i].occurance;
}
return { occurance:totalOccurance };
}
//combine both function into operation and output result into wordOccurance collection
db.project.mapReduce(
map,
reduce,
{
query: {"projects.log.subject":"~~~~~"},
out: "wordOccurance"
}
)
//query the result output
db.wordOccurance.find()
您可以更改mapreduce下的查询,以匹配您想要字数的主题。如果我的mapreduce函数不符合您的预期结果,请告诉我。
您还可以参考以下两页来创建地图和缩小功能并进行故障排除: