Project multiple fields in $ lookup mongodb

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

The entire document is recorded in the field "inventory_docs". Is there a way to compress or program a document to be written to inventory_docs

db.orders.aggregate([{
  $lookup: {
    from: "inventory",
    localField: "item",
    foreignField: "sku",
    as: "inventory_docs"
  }
}])

What I am saying is rather than that;

{
  "_id": 1,
   "item": "abc",
  "price": 12,
  "quantity": 2,
  "inventory_docs": [
    { "_id": 1, "sku": "abc", description: "product 1", "instock": 120 }
  ]
}

I want to create it as:

{
  "_id": 1,
  "item": "abc",
  "price": 12,
  "quantity": 2,
  "inventory_docs": [
    { "_id": 1, "instock": 120 }
  ]
}

How can i do this?

+4
source share

Source: https://habr.com/ru/post/1666387/


All Articles