Oct*_*ita 6 javascript bulkinsert upsert mongoose mongodb
我正在使用Mongoose .insertMany方法用对象数组填充imagesSchema。
想知道如何使用upsert:true和.insertMany()或其他方法来更新/删除所有对象,以防第二次运行此函数。
另外,我们如何禁用默认MongoDB ObjectID的Generation和_v?
Schema
const imagesSchema = new mongoose.Schema(
{
catalogue: String,
productID: String,
position: Number,
id: Number,
name: String,
alttag: String,
file: String,
type: String,
saved: String,
status: String
},
{
collection: "images",
_id: false,
upsert: true
}
);
//things that do not work
imagesSchema.plugin(uniqueValidator);
const Images = mongoose.model("Images", imagesSchema);
module.exports = Images;
Run Code Online (Sandbox Code Playgroud)
Code
Catalogue.findOne({ domain: userApiProducts.domain }, "allProducts", function(
err,
products
) {
if (err) console.error(err);
(function() {
var productsARR = [];
var productsOBJ = { data: [] };
new Promise(function(resolve, reject) {
resolve();
})
.then(function() {
var id = 0;
products.allProducts.forEach(function(x) {
var catalogueID = x._id;
for (var i = 0; i < x.media_gallery_entries.length; i++) {
var filename = x.media_gallery_entries[i].file;
function type() {
return filename
.substr((~-filename.lastIndexOf(".") >>> 0) + 2)
.toUpperCase();
}
id++;
productsARR.push({
catalogue: products._id,
productID: x._id,
position: x.media_gallery_entries[i].position,
id: id,
name: x.name,
alttag: x.media_gallery_entries[i].label,
file: x.media_gallery_entries[i].file,
type: type(),
saved: "0.00 KB",
status: "SYNCING"
});
}
});
if (productsARR.length === productsARR.length) {
productsOBJ.data = productsARR;
var arr = productsOBJ.data;
Images.insertMany(arr, function(error, docs) {});
}
})
.catch(function(e) {
console.log(e);
});
})();
});
Run Code Online (Sandbox Code Playgroud)
Output
{
"_id" : ObjectId("58795c440d5e554357bfb155"),
"__v" : 0,
"catalogue" : "5879356f8d94cf6a32cd7536",
"productID" : "58795c440d5e554357bfb143",
"position" : 2,
"id" : 17,
"name" : "Al treilea",
"alttag" : null,
"file" : "/g/r/grafolio_angel_and_devil_thumbnail_1_1_1_3_3_3.jpg",
"type" : "JPG",
"saved" : "0.00 KB",
"status" : "SYNCING"
},
{
"_id" : ObjectId("58795c440d5e554357bfb153"),
"__v" : 0,
"catalogue" : "5879356f8d94cf6a32cd7536",
"productID" : "58795c440d5e554357bfb142",
"position" : 2,
"id" : 15,
"name" : "Al treilea",
"alttag" : null,
"file" : "/g/r/grafolio_angel_and_devil_thumbnail_1_1_1_1.jpg",
"type" : "JPG",
"saved" : "0.00 KB",
"status" : "SYNCING"
} //etc
Run Code Online (Sandbox Code Playgroud)
输出正确,但是我需要:
希望有人已经从事过类似的工作,并找到了完美的解决方案!
小智 1
要禁用 mongodb 中的自动功能,ObjectId(autoIndexId)有两种方法。
_id字段的自动生成,您可以在插入文档时将其显式设置为您选择的值。例如:db.demoCollection.insertOne({ _id: "myCustomID", name: "John Wick", age: 30 });
Run Code Online (Sandbox Code Playgroud)
{ collection: 'youCollection', autoIndexId: false}.
Run Code Online (Sandbox Code Playgroud)
要_v在 mongodb 中禁用,请在定义模式时编写以下内容:
const mySchema = new mongoose.Schema({
// Your schema fields here...
}, { versionKey: false });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |