Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

$lookup mongodb

db.orders.insertMany( [
   { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
   { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
   { "_id" : 3  }
] )


db.inventory.insertMany( [
   { "_id" : 1, "sku" : "almonds", "description": "product 1", "instock" : 120 },
   { "_id" : 2, "sku" : "bread", "description": "product 2", "instock" : 80 },
   { "_id" : 3, "sku" : "cashews", "description": "product 3", "instock" : 60 },
   { "_id" : 4, "sku" : "pecans", "description": "product 4", "instock" : 70 },
   { "_id" : 5, "sku": null, "description": "Incomplete" },
   { "_id" : 6 }
] )

/* 
Next aggregate:
  1) iterates over all docums from "orders"
  2) get from collection "inventory" field "sku", check value
  3) get from collection "orders" field "item", check value
  4) if values are equal - get full docum from "inventory" and add 
    to "inventory_docs" list of docums
  5) result of aggregate will be 
  [
    {
      ...[element from "orders" collection],
      inventory_docs: [
        [list of docums from "inventory"]
      ]
    },{
      ...
    }
  ]
*/

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



[{
   "_id" : 1,
   "item" : "almonds",
   "price" : 12,
   "quantity" : 2,
   "inventory_docs" : [
      { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 }
   ]
}
{
   "_id" : 2,
   "item" : "pecans",
   "price" : 20,
   "quantity" : 1,
   "inventory_docs" : [
      { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 }
   ]
}
{
   "_id" : 3,
   "inventory_docs" : [
      { "_id" : 5, "sku" : null, "description" : "Incomplete" },
      { "_id" : 6 }
   ]
}]
Comment

lookup mongodb

let videos= await Beg.aggregate([
        {
          $project: {
            _id: 1,
          },
        },
        {
          $lookup: {
            from: "videos",
            localField: "_id",
            foreignField: "begId",
            as: "videos",
          },
        },
      ]);
Comment

PREVIOUS NEXT
Code Example
Javascript :: Javascript random password generator Exampe 
Javascript :: datatable get row data 
Javascript :: event delegation in javascript 
Javascript :: how to erase spaces from a string javascript 
Javascript :: dynamic loop variable .each create hash javascript 
Javascript :: jquery li count in ul 
Javascript :: how to use useparams in react 
Javascript :: nodejs user input 
Javascript :: check many keys in objects 
Javascript :: react inject component into another component 
Javascript :: checkbox set checked jquery 
Javascript :: urlencoded limit nodejs 
Javascript :: confirm javascript 
Javascript :: placeholder in angular 9 select 
Javascript :: json placholder 
Javascript :: node js async delay 
Javascript :: how to set input date to today date on initialization 
Javascript :: javascript reduce 
Javascript :: .join in javascript 
Javascript :: check if value is boolean 
Javascript :: difference between the `.append()` method and the `.appendChild()` method 
Javascript :: tinymce event on change 
Javascript :: how to assign value to variable 
Javascript :: how to format an integer with a comma in javascript 
Javascript :: npx http server 
Javascript :: force rerender react 
Javascript :: for in react 
Javascript :: os module in node js 
Javascript :: what is startof() in moment 
Javascript :: window.scrollto(0 0) not working 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =