2017-10-02 5 views
0

lodashにはkeybyという名前の関数があります https://lodash.com/docs/4.17.4#keyBy そのような結果を得るにはどうすればいいですか?mongoクエリから配列の代わりにオブジェクトの "keyBy"として結果を受け取る方法は?

const data = db.collection.find() 


[ 
    { id: 'a', text: 'text' }, 
    { id: 'b', text: 'text' }, 
    { id: 'c', text: 'text' }, 
    { id: 'd', text: 'text' }, 
    { id: 'e', text: 'text' }, 
] 

lodash.keyBy(data, 'id') 

{ 
    a: { id: 'a', text: 'text' }, 
    b: { id: 'b', text: 'text' }, 
    c: { id: 'c', text: 'text' }, 
    d: { id: 'd', text: 'text' }, 
    e: { id: 'e', text: 'text' }, 
} 

答えて

0

MongoDBの検索クエリでオブジェクトを取得できません。 しかし、あなたはそれを行うことはできません「keyBy」

const data = db.collection.find({}, function(err,results){ 
    if(err) throw err; 
     return lodash.keyBy(results, 'id'); 
}); 
+0

ので、集約フレームワークをlodashを使用して、1を構築することができますか?私はレコードを最初に引っ張ってから鍵をかける必要があります – crapthings

+0

@crapthingsはい –

関連する問題