Node.js 在MongoDB中查询数组
我在MongoDB有这个收藏:Node.js 在MongoDB中查询数组,node.js,mongodb,express,Node.js,Mongodb,Express,我在MongoDB有这个收藏: { "_id" : ObjectId("5df013b10a88910018267a89"), "StockNo" : "33598", "Description" : "some description", "detections" : [ { "lastDetectedOn" : ISODate("2020-01-29T04:36:41.191+0000"),
{
"_id" : ObjectId("5df013b10a88910018267a89"),
"StockNo" : "33598",
"Description" : "some description",
"detections" : [
{
"lastDetectedOn" : ISODate("2020-01-29T04:36:41.191+0000"),
"lastDetectedBy" : "comp-t",
"_id" : ObjectId("5e3135f68c9e930017de8aec")
},
{
"lastDetectedOn" : ISODate("2019-12-21T18:12:06.571+0000"),
"lastDetectedBy" : "comp-n",
"_id" : ObjectId("5e3135f68c9e930017de8ae9")
},
{
"lastDetectedOn" : ISODate("2020-01-29T07:36:06.910+0000"),
"lastDetectedBy" : "comp-a",
"_id" : ObjectId("5e3135f68c9e930017de8ae7")
}
],
"createdAt" : ISODate("2019-12-10T21:52:49.788+0000"),
"updatedAt" : ISODate("2020-01-29T07:36:22.950+0000"),
"__v" : NumberInt(0)
}
我想通过StockNo搜索并获得上次检测到它的计算机的名称(lastDetectedBy),前提是lastDetectedOn在使用Express的node.js中的Mongoose的最后5分钟内
我还有以下收藏:
{
"_id" : ObjectId("5df113b10d35670018267a89"),
"InvoiceNo" : "1",
"InvoiceDate" : ISODate("2020-01-14T02:18:11.196+0000"),
"InvoiceContact : "",
"isActive" : true
},
{
"_id" : ObjectId("5df013c90a88910018267a8a"),
"InvoiceNo" : "2",
"InvoiceDate" : ISODate("2020-01-14T02:18:44.279+0000"),
"InvoiceContact : "Bob Smith",
"isActive" : true
},
{
"_id" : ObjectId("5e3096bb8c9e930017dc6e20"),
"InvoiceNo" : "3",
"InvoiceDate" : ISODate("2020-01-14T02:19:50.155+0000"),
"InvoiceContact : "",
"isActive" : true
}
我想用isActive equals true到isActive equals false更新所有在过去30秒内(或从现在到过去某个时间的任何日期范围)发出的带有空发票联系人的文档。例如,第一条记录在过去30秒内发出,没有InvoiceContact,isActive为真,因此必须更新,但由于不同的原因,下两条记录将保持不变,第二条记录有InvoiceContact,第三条记录超出范围。第一部分
var mins5 = new Date(ISODate() - 1000* 60 * 5 )
db.getCollection('user').find({$and:[
{ "StockNo":"33598"},
{"detections.lastDetectedOn" : { $gte : mins5 }}
]})
.map(function(list){
var results = [];
list.detections.forEach(function (detections){
if(detections.lastDetectedOn > mins5){
results.push(detections.lastDetectedBy);
}
})
return results;
});
第二部分可以通过使用update而不是find的类似查询来解决