如果文档已经存在,如何从 Mongoose 时间戳中检查?
How to check from Mongoose timestamps if the document already exists?
我正在使用 node.js + puppeteer + mongoose 构建网络抓取工具。我正在从网页获取数据,并且能够将其保存到数据库中。下一步是能够检查该文档是否已存在于数据库中。一直在搜索和尝试许多方法但没有成功。
这是我的代码将数据保存到数据库的部分:
try {
const newCar = new Car({
make: make,
model: model,
year: year,
km: km,
price: price
});
let saveCar = await newCar.save();
console.log(saveCar);
console.log('car saved!');
} catch (err) {
console.log('err' + err);
}
在我的架构中,我添加了时间戳选项:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const carSchema = new Schema({
make: {
type: String
},
model: {
type: String
},
year: {
type: String
},
km: {
type: String
},
price: String
}, {timestamps: true });
module.exports = mongoose.model('Car', carSchema);
所以我希望有人能用这个把我推向正确的方向。有没有办法使用 createdAt 时间戳来检查文档是否已经在数据库中并在抓取时跳过它?
编辑。我一直在尝试使用该哈希来解决这个问题。这是我的代码:
const hash = md5(assetsUrl);
const existingCar = Car.find({
'hash': { $exists: true }
});
if (!existingCar) {
try {
const newCar = new Car({
make: make,
model: model,
year: year,
km: kmInt,
price: priceInt,
currency: currencyString,
carUrl: carUrl,
imageUrl: imageUrls,
hash: hash
});
let saveCar = await newCar.save();
console.log(saveCar);
console.log('car saved!');
} catch (err) {
console.log('err' + err);
}
} else {
console.log('car already in db');
}
这不行,代码每次都落到else块。我在这里错过了什么?
有很多可能的方法来处理您的情况:
1.Create 记录上的唯一索引 here is more 将验证数据库中的数据排他性。在您的情况下,这意味着您可以跳过其他逻辑并继续解析已保存的文档,因为没有数据会加倍。
2。每次访问页面时创建页面哈希,并将哈希存储在数据库中。可以找到更多 here or here 。在您的特定情况下,您可以在第一次访问时创建页面哈希,然后验证内容是否已从数据库中的哈希更改。如果是这样,请进行解析,如果不是,请跳过页面。
3。如果您只是想验证数据库中是否没有相同的数据并且不想添加唯一索引,则必须先 findOne
用于数据库中的相同数据。可以找到更多 here
我正在使用 node.js + puppeteer + mongoose 构建网络抓取工具。我正在从网页获取数据,并且能够将其保存到数据库中。下一步是能够检查该文档是否已存在于数据库中。一直在搜索和尝试许多方法但没有成功。 这是我的代码将数据保存到数据库的部分:
try {
const newCar = new Car({
make: make,
model: model,
year: year,
km: km,
price: price
});
let saveCar = await newCar.save();
console.log(saveCar);
console.log('car saved!');
} catch (err) {
console.log('err' + err);
}
在我的架构中,我添加了时间戳选项:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const carSchema = new Schema({
make: {
type: String
},
model: {
type: String
},
year: {
type: String
},
km: {
type: String
},
price: String
}, {timestamps: true });
module.exports = mongoose.model('Car', carSchema);
所以我希望有人能用这个把我推向正确的方向。有没有办法使用 createdAt 时间戳来检查文档是否已经在数据库中并在抓取时跳过它?
编辑。我一直在尝试使用该哈希来解决这个问题。这是我的代码:
const hash = md5(assetsUrl);
const existingCar = Car.find({
'hash': { $exists: true }
});
if (!existingCar) {
try {
const newCar = new Car({
make: make,
model: model,
year: year,
km: kmInt,
price: priceInt,
currency: currencyString,
carUrl: carUrl,
imageUrl: imageUrls,
hash: hash
});
let saveCar = await newCar.save();
console.log(saveCar);
console.log('car saved!');
} catch (err) {
console.log('err' + err);
}
} else {
console.log('car already in db');
}
这不行,代码每次都落到else块。我在这里错过了什么?
有很多可能的方法来处理您的情况:
1.Create 记录上的唯一索引 here is more 将验证数据库中的数据排他性。在您的情况下,这意味着您可以跳过其他逻辑并继续解析已保存的文档,因为没有数据会加倍。
2。每次访问页面时创建页面哈希,并将哈希存储在数据库中。可以找到更多 here or here 。在您的特定情况下,您可以在第一次访问时创建页面哈希,然后验证内容是否已从数据库中的哈希更改。如果是这样,请进行解析,如果不是,请跳过页面。
3。如果您只是想验证数据库中是否没有相同的数据并且不想添加唯一索引,则必须先 findOne
用于数据库中的相同数据。可以找到更多 here