Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Problem sending to mongo with a variable containing an array of objects

How to insert into an array of objects There is an array of objects, and I want to put them all in one query, and I fail, here's my schema

const userSchema = new Schema({
    user: {
        type: String,
        required: true
    },
    roles: {
        User: {
            type: Number,
            default: 2001
        },
        Editor: Number,
        Admin: Number
    },
    password: {
        type: String,
        required: true
    },
    
    Stock:[{
        sku:String,
        productname:String,
        sendout:Number,
        recived:Number,
        totalinstock:Number,
        location:String

    }],
    Orders:[{
        date:Date,
        OrderNumber:Number,
        Address:String,
        Phone:Number,
        Country:String,
        Name:String,
        Trackingnumber:Number,
        ZipCode:Number,
        Province:String,
        Quantity:Number,
        Product_Name:String,
        SKU:String
    }],

  
});

module.exports = mongoose.model('User', userSchema);

What I did was I took the orders I wanted to send to DB and merged them into an object

const producktarray = [
  {
    OrderNumber: '1468313738187862019',
    Address: 'bbbbbbbb',
    SKU: [ 'CJNSXZXX00480' ],
    Quantity: [ 9 ],
    Province: 'yyyy',
    Product_Name: [ 'Thick Loose ' ],
    Name: 'xxxxx'
  },
  {
    OrderNumber: 546546456456,
    Address: 'vvvv,
    SKU: [ 'CJNS' ],
    Quantity: [ 6 ],
    Province: 'New York',
    Product_Name: [ 'Thick Loose ' ],
    Name: 'Gggggggg'
  }
]

Now that I'm trying to send it to Mongo I get errors The way I tried to put it is (I also tried updateOne)

await User.updateMany({
  $push :{
  Orders:
  { 
    producktarray
  
}}});

I want all the data to go into OrdersI expect to see in the mango

"Orders": [
    {
    "OrderNumber": '1468313738187862019',
    "Address": 'bbbbbbbb',
    "SKU": [ 'CJNSXZXX00480' ],
    "Quantity": [ 9 ],
    "Province": 'yyyy',
    "Product_Name": [ 'Thick Loose Three-dimensional Japanese Linen Pants' ],
    "Name": 'xxxxx'
     "_id": {
        "$oid": "62826016005ce00c5f0235c8"
      }
    },
    {
      OrderNumber: 546546456456,
    Address: 'vvvv,
    SKU: [ 'CJNS' ],
    Quantity: [ 6 ],
    Province: 'New York',
    Product_Name: [ 'Thick Loose Three-dimensional Japanese Linen Pants' ],
    Name: 'Gggggggg'
    "_id": {
        "$oid": "62826035005ce00c5f0235cc"
      }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

if you define the array of the object schema, so create a different schema for an array of objects and assign the key of the Value

const orderSchema=newSchema({
      date: {
        type: Date,
        default: newDate()
      },
      OrderNumber: {
        type: Number,
        required: true
      },
      Address: {
        type: String,
        required: true
      },
      Phone: {
        type: Number
      },
      Country: {
        type: String
      },
      Name: {
        type: String,
        required: true
      },
      Trackingnumber: {
        type: Number
      },
      ZipCode: {
        type: Number
      },
      Province: {
        type: String,
        required: true
      },
      Quantity: {
        type: [
          Number
        ],
        required: true
      },
      Product_Name: {
        type: [
          String
        ],
        required: true
      },
      SKU: {
        type: [
          String
        ],
        required: true
      }
    });
const userSchema=newSchema({
      user: {
        type: String,
        required: true
      },
      roles: {
        User: {
          type: Number,
          default: 2001
        },
        Editor: Number,
        Admin: Number
      },
      password: {
        type: String,
        required: true
      },
      Stock: [
        {
          sku: String,
          productname: String,
          sendout: Number,
          recived: Number,
          totalinstock: Number,
          location: String
        }
      ],
      Orders: [
        orderSchema
      ],
      
    });
module.exports=mongoose.model('User',
    userSchema);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!