简体   繁体   中英

Fetching data from one table to another in MongoDb

I am having a Database in mongo named "sell". Now this DB contains two tables "Car" and "Order". In "Car" table I have attribute named "price". Now when i am executing

db.Order.aggregate([ 
{
                    $lookup: {
                        from: "Car",
                        localField: "carId",
                        foreignField: "_id",
                        as: "PRICE",
                    },
                },
                { $unwind: "$PRICE" },]).pretty()

command in mongo shell it gives output containing "Order" table attributes with PRICE array which contains all the attributes of "Car" table. Now, I want to fetch "price" attribute which is in "Car" table. My current output is as shown in link below but I want to fetch "price" from "PRICE". So what should my command be???

My code for "Car" table model is

module.exports = {
    tableName: "Car",
    schema: true,
    attributes: {
        carmodel: {
            type: "String",
        },
        stock: {
            type: "Number",
        },
        color: {
            model: "Master",
        },
        code: {
            type: "STRING",
            unique: true,
        },
        brakes: {
            type: "String",
        },

        description: {
            type: "String",
        },
        wheels: {
            type: "String",
        },
        carwindow: {
            type: "String",
        },
        carseat: {
            model: "Master",
        },
        engine: {
            type: "String",
        },
        price: {
            type: "Number",
        },
        //self relation
        parentId: {
            model: "Car",
            columnName: "parentId",
        },
        //OTM
        Master: {
            collection: "Master",
            via: "car",
        },
        //OTO
        order: {
            collection: "order",
            via: "carId",
        },
        sellprice: {
            collection: "order",
            via: "price",
        },
    },
};

My code for "Order" table is

const { date } = require("faker");
const Car = require("../models/Car");

module.exports = {
    tableName: "Order",
    schema: true,
    attributes: {
        address: { type: "String" },
        status: {
            model: "Master",
        },
        orderDate: { type: "string", columnType: "datetime" },
        //OTO
        carId: {
            model: "Car",
            unique: true,
        },
        price: {
            model: "Car",
            unique: true,
        },
        //OTM
        Master: {
            collection: "Master",
            via: "order",
        },
    },
};

My desired output is

{"PRICE" : 905000 }

PRICE is total of all cars price in Order

https://i.stack.imgur.com/sJYDT.jpg

db.Order.aggregate([
    {
        $lookup: {
            from: "Car",
            let: { carId: "$carId" },
            pipeline: [
                { $match: { $expr: { $eq: ["$$carId", "$_id"] } } },
                { $project: { price: 1, _id: 0 } },
            ],
            as: "carInfo",
        },
    },
    { $unwind: "$carInfo" },
    { $replaceRoot: { newRoot: "$carInfo" } },
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM