Mongoose - Generate ObjectID for each object before saving document

前端 未结 2 746
遥遥无期
遥遥无期 2021-01-15 15:43

I want generate an ObjectID for each Object present inside my array. The thing is I\'m getting the products with a .forEach statement from another server and push them insid

2条回答
  •  梦毁少年i
    2021-01-15 15:58

    If you want to add ObjectId automatically, you need to define a separate schema for it and set the _id options for the schema as true.

    Do the following:

    • Change your productsSchema as CatalogueSchema (for ease of understanding).
    • Define a new ProductSchema for Product (element of allProducts)
    • In CatalogueSchema define allProducts type as [Product.schema]. This will automatically add _id (ObjectId).

    Also, you don't need to add created_at and updated_at as part of schema when you set timestamps option as true.

    Catalogue Schema

    const Product = require('Product_Schema_Module_Path'); // Edit
    
    const CatalogueSchema = new mongoose.Schema({
    
        apiKey: String,
        domain: String,
        totalcount: Number,
        totaldone: Number,
        allSKUS: Array,
        allProducts: [Product.schema]   
        // Note the change here (Array -> [Product.schema]
      // Creating a separate schema ensures automatic id (ObjectId)
    
    }, { collection: 'catalogue', timestamps: true });
    
    CatalogueSchema.plugin(uniqueValidator);
    
    const Catalogue = mongoose.model('Catalogue', CatalogueSchema);
    module.exports = Catalogue;
    

    Product Schema (New schema to ensure adding of ObjectId)

    const ProductSchema = new mongoose.Schema({
    
        id: Number,
        sku: String,
        name: String,
        status: Number,
        total_images: Number,
        media_gallery_entries: Array
    
    }, { _id: true, timestamps: true });  
    // _id option is true by default. You can ommit it.
    // If _id is set to false, it will not add ObjectId
    
    ProductSchema.plugin(uniqueValidator);
    
    const Product = mongoose.model('Product', ProductSchema);
    module.exports = Product;
    

    EDIT (Save Products in Catalogue)

    (Also, note that you have to require the ProductSchema module in your CatalogueSchema module)

    // Map userApiProducts.allProducts to array of Product documents
    const products = userApiProducts.allProducts.map(product => {
        return new Product(product);
    })
    
    const newProduct = {
        apiKey: userApiProducts.apiKey,
        domain: userApiProducts.domain,
        totalcount: userApiProducts.totalcount,
        totaldone: userApiProducts.totaldone,
        allSKUS: userApiProducts.allSKUS,
        allProducts: products
    };
    
    Catalogue
        .findOneAndUpdate({ domain: userApiProducts.domain }, newProduct, { upsert:true } , (err, products) => {
        // Handle error
    });
    

提交回复
热议问题