Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

join in mongodb

// Join in mongoDB

// Customer table
db.customer.insertMany(
    [
        { "name": "Abhishek", "age": 50, "address": "f/403 anurag", productId: 1 },
        { "name": "raj", "age": 30, "address": "302 Neelmani", productId: 2 },
        { "name": "simran", "age": 70, "address": "rajkot", productId: 3 },
        { "name": "Dhruval", "age": 60, "address": "chennai", productId: 4 }
    ]
)

// Product table
db.product.insertMany(
    [
        { "product_name": "Abhishek", prod_id: 2, "price": 300 },
        { "product_name": "raj", prod_id: 3, "price": 500 },
        { "product_name": "simran", prod_id: 1, "price": 400 },
        { "product_name": "Dhruval", prod_id: 4, "price": 1000 }
    ]
)

// Join using lookUp
db.customer.aggregate([
    {
        $lookup:
        {
            from: "product",
            localField: "productId",
            foreignField: "prod_id",
            as: "productReference"
        }
    }
])
 
PREVIOUS NEXT
Tagged: #join #mongodb
ADD COMMENT
Topic
Name
9+6 =