English 中文(简体)
Mongoose - 插入Manymeth, upsert:true?
原标题:Mongoose - InsertMany Method, upsert:true?

I m 采用Mongoose .insertMany方法,用多种物体填入imagesSchema

Wondering how to go about using upsert:true with .insertMany() or another method to update/remove all objects in case this function is run a second time.

Also how can we Disable Default MongoDB ObjectID s Generation and _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;

法典:

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);
      });
  })();
});

      

产出:

{
    "_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

产出是正确的,但我需要:

  • Find a way to use upsert:true or find and Remove all Items...
  • Disable ObjectID Generation
  • Disable _v Generation

希望者已经做了类似的工作,找到了解决这一问题的完美办法!

问题回答

为拆卸汽车<代码>ObjectId(autoIndexId),在mongodb有两种方式。

  1. If you want to disable the automatic generation of the _id field, you can explicitly set it to a value of your choice when inserting documents. For example:
db.demoCollection.insertOne({ _id: "myCustomID", name: "John Wick", age: 30 });
  1. Or you can turn it off by defining this in your schema:
    { collection:  youCollection , autoIndexId: false}.

About autoindexid in official doc.

http://www.un.org

const mySchema = new mongoose.Schema({
  // Your schema fields here...
}, { versionKey: false });




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签