mongodb查询是否未检查打印机?
我有一个打印机集合和printerInspection集合,其中包含打印机id和检查日期。mongodb查询是否未检查打印机?,mongodb,Mongodb,我有一个打印机集合和printerInspection集合,其中包含打印机id和检查日期。 如何找到在2019年8月23日未检查的打印机(在本例中,只有打印机2和1在2019年8月23日检查过。。。 我目前的解决方案是用java拉取集合和过滤,但速度非常慢,正在寻找mongo查询解决方案 db={ "printer": [ { "printerId": 0 }, { "printerId": 1 }, { "prin
如何找到在2019年8月23日未检查的打印机(在本例中,只有打印机2和1在2019年8月23日检查过。。。 我目前的解决方案是用java拉取集合和过滤,但速度非常慢,正在寻找mongo查询解决方案
db={
"printer": [
{
"printerId": 0
},
{
"printerId": 1
},
{
"printerId": 2
},
{
"printerId": 3
},
{
"printerId": 4
},
],
"printerInspection": [
{
"printerId": 2,
"date": "08-23-2019"
},
{
"printerId": 2,
"date": "08-22-2019"
},
{
"printerId": 2,
"date": "08-21-2019"
},
{
"printerId": 1,
"date": "08-23-2019"
},
{
"printerId": 3,
"date": "08-22-2019"
},
{
"printerId": 3,
"date": "08-21-2019"
}
]
}
使用聚合管道,如下所示 1.我们将所有可用的打印机和在指定日期接受检查的打印机归为俱乐部所有 2.打印机阵列与被检打印机的区别
db.collection.aggregate([
{
$project: {
printers: {
$map: {
input: "$printer",
as: "item",
in: "$$item.printerId"
}
},
inspectedPrinters: {
$map: {
input: {
$filter: {
input: "$printerInspection",
as: "item",
cond: {
$eq: [
"$$item.date",
"08-23-2019"
]
}
}
},
as: "item2",
in: "$$item2.printerId"
},
}
}
},
{
$project: {
notInspected: {
$setDifference: [
"$printers",
"$inspectedPrinters"
]
}
}
}
])
在链接中,您定义了db={printer:[]},并且在访问db.printer.aggregate([])时,它只访问打印机阵列,这就是数据为空的原因。您可以向我显示新链接吗?如果它工作,我将接受。