当我输入userId时,我想获得开始和结束日期之间的所有通知,这些通知与一个注册表相关。
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const RegisterSchema = new Schema({
userId: {type: Schema.Types.ObjectId, required: true},
accessToken: {type:String, required: true, default: null},
})
module.exports = Register = mongoose.model( 'register', RegisterSchema)
以下是一些寄存器数据
[
{
"_id": "5eac9e815fc57b07f5d0d29f",
"userId": "5ea108babb65b800172b11be",
"accessToken": "111"
},
{
"_id": "5ecaeba3c7b910d3276df839",
"userId": "5e6c2dddad72870c84f8476b",
"accessToken": "222"
}
]
下一个文档包含了与Register模式相关的数据,这些数据通过accessToken
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const NotificationSchema = new Schema({
accessToken: {type:String, required: true},
summaryId: {type:Number, required: true},
dateCreated: {type: Date, default: Date.now},
})
module.exports = Notification = mongoose.model( 'notification', NotificationSchema)
以下是一些通知数据
[{
"_id": "5ebf0390c719e60004f42e74",
"accessToken": "111",
"summaryId": 1111,
"dateCreated": "17 Apr 2020" },
{
"_id": "6ebf0390c719e60004f42e76",
"accessToken": "222",
"summaryId": 2221,
"dateCreated": "18 Apr 2020" },
{
"_id": "6ebf0390c719e60004f42e78",
"accessToken": "111",
"summaryId": 1112,
"dateCreated": "25 May 2020" },
{
"_id": "6ebf0390c719e60004f42e80",
"accessToken": "222",
"summaryId": 2222,
"dateCreated": "26 May 2020" }
]
var userId = '5ea108babb65b800172b11be'
var dateStart = '27 Apr 2020';
var dateEnd = '27 May 2020';
var match = {$match: { userId: mongoose.Types.ObjectId(userId) } };
var lookup ={
$lookup:
{
from: "notifications",
localField: "accessToken",
foreignField: "accessToken",
as: "testingThis"
}
};
project = {
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: { {"dateCreated": {'$gte': dateStart, '$lte': dateEnd }} }
}
}
}
};
var agg = [
match,
lookup,
project
];
Register.aggregate(agg)
.then( events => {
if(events){
return resolve(events);
}else{
return reject({success:false});
}
})
.catch(err => {
console.log('ERROR ' + JSON.stringify(err.message));
return reject({success:false});
})
我希望看到5月25日的通知,访问令牌为111,但我得到一个错误。
ERROR : {"\"An object representing an expression must have exactly one field: { $gte: new Date(1588017802546), $lte: new Date(1590609802546) }\""}
我消除了这个错误......但仍然没有得到任何返回。
var dateCondition = { $and: [
{ $gte: [ "$$item.dateCreated", dateStart.getTime() ] },
{ $lte: [ "$$item.dateCreated", dateEnd.getTime() ] }
] }
project = {
$project: {
items: {
$filter: {
input: "$items",
as: "item",
cond: dateCondition
}
}
}
};
这是我的项目的样子。
{
"$project": {
"items": {
"$filter": {
"input": "$items",
"as": "item",
"cond": {
"$and": [
{"$gte": ["$$item.dateCreated",1588019227296] },
{"$lte": ["$$item.dateCreated",1590611227296] }
] } } } }
}
利用评论中的建议... 我把 "项目"(从尝试2)改为 "通知"。
var dateCondition = { $and: [
{ $gte: [ "$$item.dateCreated", dateStart.getTime() ] },
{ $lte: [ "$$item.dateCreated", dateEnd.getTime() ] }
] }
project = {
$project: {
notifications: {
$filter: {
input: "$notifications",
as: "item",
cond: dateCondition
}
}
}
};
还是不行
所以,为了尽量简化,让它正常工作... ... 我试着用summary id
dateCondition = { $and: [
{ $gte: [ "$$item.summaryId", 1 ] },
{ $lte: [ "$$item.summaryId", 555555 ] }
] }
project = {
$project: {
notifications: {
$filter: {
input: "$notifications",
as: "item",
cond: dateCondition
}
}
}
};
这工作... 所以我认为这是一个日期问题。
// make sure the input dates are REALLY date objects
var dateStart = new Date(inputDateStart);
var dateEnd = new Date(inputDateEnd);
var match = {$match: { userId: mongoose.Types.ObjectId(userId) } };
var lookup ={
$lookup:
{
from: "my_Notifications",
localField: "accessToken",
foreignField: "accessToken",
as: "notifications"
}
};
var dateCondition = { $and: [
{ $gte: [ "$$item.dateCreated", dateStart ] },
{ $lte: [ "$$item.dateCreated", dateEnd ] }
]}
project = {
$project: {
notifications: {
$filter: {
input: "$notifications",
as: "item",
cond: dateCondition
} } }
};
var agg = [
match,
lookup,
project
];
Register.aggregate(agg)
.then( ..... )
你的解决方案看起来几乎是正确的,前提是 dateStart
和 dateStart
实际上 Date
对象而不是 String
s.
你的 试试2 是不完整的,我不确定它是否使用了的。$lookup
从 试试1 或不。如果是这样,你必须确保输出的 $lookup
的输入相同。$filter
. 所以你应该改变 as
在 $lookup
匹配 input
的 $filter
{
$lookup: {
from: "notifications",
localField: "accessToken",
foreignField: "accessToken",
as: "items" // here
}
}
替代方案
我不知道你想要什么输出。如果你只需要数组通知,而不需要用户对象,你可以试试下面的方法。
[{
$match: { userId: mongoose.Types.ObjectId(userId) }
}, {
$lookup: {
from: "notifications",
localField: "accessToken", // don't forget to index register.accessToken
foreignField: "accessToken", // don't forget to index notification.accessToken
as: "notifications"
}
}, {
$unwind: "$notifications"
}, {
$match: {
dateCreated: { $gte: dateStart, $lte: dateEnd } // dateStart, dateEnd should be Date objects
}
}, { // optional, move notifications to top lvel
$replaceRoot: { root: '$notifications' }
}]