私はこのようなデータモデルを定義した:mongoDBに関する集約パズル?
import mongoose from 'mongoose';
const schema = mongoose.Schema({
title: {type: String, required: true},
content: {type: String, required: true},
language: {type: String, required: true},
label: [{type: String, required: false}],
createTime: {type: Date, default: Date.now},
updateTime: {type: Date, default: Date.now}
});
const Snippet = mongoose.model('Snippet', schema, 'snippet');
export default Snippet;
私が欲しいのは、私が使用している場合ということです:
Snippet.aggregate({...})
、データがある場合:
[{
title: 'a',
content: 'b',
language: 'javascript'
},{
title: 'a',
content: 'b',
language: 'javascript',
label: ['new', 'old']
},
{
title: 'a',
content: 'b',
language: 'javascript',
label: ['old']
}]
、結果はどのようなI欲しかったのは次のようなものです:
{
languageCount : [
{
language : 'javascript',
count : 3
}],
labelCount : [
{
label : 'new',
count : 1
},
{
label : 'old',
count : 2
}]
}
「...」の部分をどうやって仕上げるか分かりません。
それについて、または他の方法で考えている人は、誰でもそれを回避できます。ありがとうございます。
あなたの 'mongod'は何ですか? – styvane
私のmongodバージョンは3.2.1です。最新バージョンに基づいていくつかの機能を使用することができます、私はそれで大丈夫です。 –