[英]MongoDb query between two collections
I have this query that should merge both tables and return the result我有这个查询应该合并两个表并返回结果
db.purchasefromobjectmodel.aggregate([
{
"$lookup":{
"from":"amazonmodels",
"localField":"receiptId",
"foreignField":"receiptId",
"as" :"results"
}])
How can i edit it so that it would return me objects where purchaseDate from first table is equal to the data of second table ( or near with minor difference >24hrs ), the objects are linked through receiptId我如何编辑它,以便它返回我的对象,其中第一个表中的 purchaseDate 等于第二个表的数据(或接近但有细微差别> 24hrs ),这些对象通过receiptId 链接
edit:编辑:
const purchaseFromAppObjectModelSchema = new mongoose.Schema({
receiptId: String,
sku: String,
itemType: String,
purchaseDate: { type: Date, default: Date.now },
UserData: { type: [userDataModel] },
idfa: String,
status: String,
appName: String,
trafficSource: String,
trafficName: String,
appVersion: String,
deviceName: String,
deviceBrand: String,
});
const amazonSchema = new mongoose.Schema(
{
autoRenewing: Boolean,
betaProduct: Boolean,
cancelDate: Number,
cancelReason: Number,
deferredDate: String,
deferredSku: String,
freeTrialEndDate: Number,
gracePeriodEndDate: Number,
parentProductId: String,
productId: String,
productType: String,
purchaseDate: Number,
quantity: Number,
receiptId: String,
renewalDate: Number,
term: String,
termSku: String,
testTransaction: Boolean,
},
{ timestamps: true, toObject: { virtuals: true }, toJSON: { virtuals: true } }
);
queries i tried:我试过的查询:
let result = await purchaseFromAppObjectModel.aggregate([
{
$lookup: {
from: "amazonmodels",
localField: "purchaseDate",
foreignField: "purchaseDate",
as: "purchaseDateInfo",
},
},
// {
// $replaceRoot: {
// newRoot: {
// $mergeObjects: [{ $arrayElemAt: ["$purchaseDateInfo", 0] }, "$$ROOT"],
// },
// },
// },
// { $project: { purchaseDateInfo: 0 } },
]);
This one merges the collections with same receipt id
{
$lookup: {
from: "amazonmodels",
localField: "receiptId",
foreignField: "receiptId",
as: "purchaseDateInfo",
},
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [{ $arrayElemAt: ["$purchaseDateInfo", 0] }, "$$ROOT"],
},
},
},
{ $project: { purchaseDateInfo: 0 } },
]);
let result = await purchaseFromAppObjectModel.aggregate([
{$match : { purchaseDate : createdAt } } ,
{
$lookup: {
from: "amazonmodels",
localField: "purchaseDate",
foreignField: "purchaseDate",
as: "purchaseDateInfo",
},
},
// {
// $replaceRoot: {
// newRoot: {
// $mergeObjects: [{ $arrayElemAt: ["$purchaseDateInfo", 0] }, "$$ROOT"],
// },
// },
// },
// { $project: { purchaseDateInfo: 0 } },
]);
I think this one is the one i should try to mock:我认为这是我应该尝试模拟的:
db.orders.aggregate([
{
$lookup:
{
from: "warehouses",
let: { order_item: "$item", order_qty: "$ordered" },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$stock_item", "$$order_item" ] },
{ $gte: [ "$instock", "$$order_qty" ] }
]
}
}
},
{ $project: { stock_item: 0, _id: 0 } }
],
as: "stockdata"
}
}
])
And much more以及更多
Try this:尝试这个:
db.purchaseFromApp.aggregate([
{
$lookup: {
from: "amazonmodels",
let: {
purchaseDate: {
$dateFromString: {
dateString: { $toString: "$purchaseDate" },
format: "$Y-%m-%d"
}
},
receiptId: "$receiptId"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$receiptId", "$$receiptId"] },
{
$eq: [
{
$dateFromString: {
dateString: { $toString: "$purchaseDate" },
format: "$Y-%m-%d"
}
},
"$$purchaseDate"
]
}
]
}
}
}
],
as: "purchaseDateInfo"
}
},
// Add other stages
]);
if anyone passed by and want to check how did i do it using simple aggregation and nodejs this is how:如果有人路过并想检查我是如何使用简单的聚合和nodejs做到这一点的,这就是:
//first the query
let result = await purchaseFromAppObjectModel.aggregate([
{
$lookup: {
from: "amazonmodels",
localField: "receiptId",
foreignField: "receiptId",
as: "amazonResults",
},
},
]);
//Then looping
for (var i = 0; i <= result.length - 1; i++) {
let objResult = result[i];
console.log(
"this is the result index : ",i,"this is the size :",objResult.amazonResults.length);
let purchaseDate = objResult.purchaseDate;
if (objResult.amazonResults != 0) {
let createdAt = objResult.amazonResults[0].createdAt;
const diffTime = Math.abs(createdAt - purchaseDate);
console.log(diffTime);
if (diffTime < 5000) {
console.log("purchase date " + purchaseDate);
console.log("createdAt " + createdAt);
console.log("same date");
} else {
console.log(" different dates");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.