異なるコレクションからデータを取得して1つのオブジェクト配列として返そうとしていますが、同じタイトルのオブジェクトをマージする必要があります。JS:同一のタイトル値を持つオブジェクトを新しい配列オブジェクトにマージする
'search': function(value) {
const res1 = Col1.find({ title: new RegExp(value, 'i') }, { fields: { title: 1, type: 1 } }).fetch() || [],
res2 = Col2.find({ title: new RegExp(value, 'i') }, { fields: { title: 1, type: 1 } }).fetch() || []
let result = res1.concat(res2)
return result
}
は、最初のコレクションがtype
-fieldのために私に「車」を与えると仮定し、第二は、「動物の私を与えます。
タイトルが同じ場合は、type
-fieldを変更して内容を追加する必要があります。
{ _id: 1, title: 'mercedes', type: 'cars' }
{ _id: 2, title: 'monkey', type: 'animals' }
{ _id: 3, title: 'jaguar', type: 'cars' }
{ _id: 4, title: 'jaguar', type: 'animals' }
はあなたが同じタイトルのハッシュテーブルtitles
を使用して、配列にアイテムを割り当て、インデックスを救うことができる
{ _id: 1, title: 'mercedes', type: 'cars' }
{ _id: 2, title: 'monkey', type: 'animals' }
{ title: 'jaguar', type: 'multiple', results: [
{ _id: 3, title: 'jaguar', type: 'cars' }
{ _id: 4, title: 'jaguar', type: 'animals' }
] }
が見えますあなたのデータベースは最初とは異なっていますか? – adeneo