【发布时间】:2020-09-30 15:19:44
【问题描述】:
我是 MongoDB 的新手,我想使用 $lookup 函数聚合两个集合。
有什么方法可以将集合 A 中的字段与集合 B 中的字段在 x 范围内进行比较?
例如:
集合 A 的时间(下面代码中名为时间戳的变量)信息是 12:00:00
集合 B 的时间(下面代码中名为时间戳的变量)信息是 12:01:00
我想在 1 秒的范围内聚合文档。
这是我当前的代码示例:
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://ipaddress/tms";
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("tms");
dbo.collection('transaction_logs').aggregate([
{
$lookup:
{
from: 'transactionLogs',
localField: 'deviceSn' && 'deviceName' && 'timestamp',
foreignField: 'deviceSn' && 'deviceName' && 'timestamp',
as: 'transactionLogs_docs'
}
},
{ $out: "integrated_transaction_logs" }
]).toArray(function (err, res) {
if (err) throw err;
console.log(JSON.stringify(res));
db.close();
});
});
【问题讨论】: