【问题标题】:Fetching data from one table to another in MongoDb在 MongoDb 中从一个表中获取数据到另一个表
【发布时间】:2020-12-17 07:21:29
【问题描述】:

我在 mongo 中有一个名为“sell”的数据库。现在这个 DB 包含两个表“Car”和“Order”。在“汽车”表中,我有名为“价格”的属性。现在当我执行时

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

mongo shell 中的命令,它提供包含“Order”表属性的输出,其中 PRICE 数组包含“Car”表的所有属性。现在,我想获取“汽车”表中的“价格”属性。我当前的输出如下面的链接所示,但我想从“价格”中获取“价格”。那我的命令应该是什么???

我的“汽车”表模型代码是

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",
        },
    },
};

我的“订单”表代码是

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",
        },
    },
};

我想要的输出是

{"PRICE" : 905000 }

PRICE 是订单中所有汽车价格的总和

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

【问题讨论】:

  • 请不要以图片形式发布一些示例数据
  • 代码示例数据或输出应该是什么样子的一些数据??
  • 最好同时发布两者
  • 先生请看一下
  • 如果您只发布数据,而不是您的结构,我可以帮助您查询,因为我不是 nodejs 人。如果不可能,有时有人会帮助你

标签: javascript node.js database mongodb sails.js


【解决方案1】:
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" } },
]);

【讨论】:

  • 这不起作用。我在我的 mongo shell 中执行了它,但没有显示输出。你能不能给我一些其他的建议?
  • 检查这个 $eq:["$$carId","$_id"] 你的 ID 是否相同,数据类型也相同
  • 缺少一个括号,所以我没有得到输出。现在我明白了,但是如果我想添加这些输出并将其显示为总价怎么办??
  • db.Order.aggregate([ { $lookup: { from: "Car", let: { car​​Id: "$carId" }, pipeline: [ { $match: { $expr: { $ eq: ["$$carId", "$_id"] } } }, { $project: { price: 1, _id: 0 } }, ], as: "carInfo", }, }, { $unwind: " $carInfo" }, { $replaceRoot: { newRoot:{$mergeObjects:[ "$carInfo",{orderPrice:"$price"}]} } }, {$addFields:{totalPrice:{$add:["$price ","$orderPrice"]}}} ]);
  • 在 mongodb 中有数百种方式,如果你喜欢我的回答,请点赞和喜欢
猜你喜欢
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多