Join di MongoDB??

ada yang punya contoh buat join di mongodb + nodejs/ tutor indonesia. soalnya saya cari adanya tutor inggris semua?

avatar wahyukouse
@wahyukouse

3 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

Dasarnya mongodb bukan relational database, untuk melakukan join bisa pake $lookup

Contoh kamu punya 2 table:

<pre> //table order [ { _id: 1, product_id: 154, status: 1 } ]

//table product [ { _id: 154, name: 'Chocolate Heaven' }, { _id: 155, name: 'Tasty Lemons' }, { _id: 156, name: 'Vanilla Dreams' } ] </pre>

Join di mongonya seperti ini

<pre> if (err) throw err;

dbo.collection('orders').aggregate([ { $lookup: { from: 'products', localField: 'product_id', //id di relationnya foreignField: '_id', // id di table productnya as: 'orderdetails' } } ]).toArray(function(err, res) { if (err) throw err; console.log(JSON.stringify(res)); db.close(); }); </pre>

contoh dari <a href='https://www.w3schools.com/nodejs/nodejs_mongodb_join.asp '>https://www.w3schools.com/nodejs/nodejs_mongodb_join.asp </a>

avatar hilmanski
@hilmanski

2670 Kontribusi 2132 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban