正在从一个 table 中获取数据到 MongoDb 中的另一个
Fetching data from one table to another in MongoDb
我在 mongo 中有一个名为“sell”的数据库。现在这个 DB 包含两个 tables“Car”和“Order”。在“汽车”table 中,我有一个名为“价格”的属性。现在当我执行
db.Order.aggregate([
{
$lookup: {
from: "Car",
localField: "carId",
foreignField: "_id",
as: "PRICE",
},
},
{ $unwind: "$PRICE" },]).pretty()
mongo shell 中的命令它给出包含“Order”table 属性的输出以及包含“Car”table 的所有属性的 PRICE 数组。现在,我想获取“Car”table 中的“price”属性。我当前的输出如下面的 link 所示,但我想从“价格”中获取“价格”。那么我的命令应该是什么???
我的“汽车”table 型号代码是
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",
},
},
};
我的“订单”代码 table 是
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
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 中有一个名为“sell”的数据库。现在这个 DB 包含两个 tables“Car”和“Order”。在“汽车”table 中,我有一个名为“价格”的属性。现在当我执行
db.Order.aggregate([
{
$lookup: {
from: "Car",
localField: "carId",
foreignField: "_id",
as: "PRICE",
},
},
{ $unwind: "$PRICE" },]).pretty()
mongo shell 中的命令它给出包含“Order”table 属性的输出以及包含“Car”table 的所有属性的 PRICE 数组。现在,我想获取“Car”table 中的“price”属性。我当前的输出如下面的 link 所示,但我想从“价格”中获取“价格”。那么我的命令应该是什么???
我的“汽车”table 型号代码是
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",
},
},
};
我的“订单”代码 table 是
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
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" } },
]);