How to import a field from one database to another?

I have two collections ( _items and _details ), and within the _details database I have a field called product_description , how would I be able to embed that field to the _items database by matching the product_barcode field as both of the databases have that field. I am trying to get the descriptions to match the barcodes in the _items database

Any help would be appreciated

Hey @samhuss123

The aggregation pipeline can help you with this.

Has you have stated both collection have a product_barcode field. Therefore, you can use $lookup to get the product associated with the items, then use $project to filter down the fields you want in the collection then use $out to save the new collection of _items.
FYI Compass makes building aggregation pipelines very easy.

db._items.aggregate([
  {
    $lookup: {
      
        from: _products,
        localField: 'product_barcode',
        foreignField: 'product_barcode',
        as: products
    }
  },
  {
    $project: {
      .... fields you want in collection
    }
  },
  {
    $out: '_items'
  }
])
1 Like

Thanks! Appreciate it, got it to work

1 Like