Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mongoose rejects an object you push into an array field

ErrorCastError: Cast to string failed for value

Short answer

Mongoose casts every pushed value to the type the schema declares. If the array is declared as [String], an object cannot be cast and the push fails with a CastError. Declare the array with a sub-schema so it becomes a document array. Also check for a key named type inside a nested object, which Mongoose reads as a field type.

  • Applies to Mongoose 8, Node.js 22, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print how Mongoose sees the field. If the type is String, or the array holds Strings, the schema is the problem, not the data you send.

    console.log(Order.schema.path('items'));
  2. 2Match what you see, then jump to its fix

    1. 1Cast to string failed ... at path "items" and the schema line reads items: [String] or items: [{ type: String }]The array is declared as [String] but you push objectsFix
    2. 2The failing field is an object with a property called type, and Mongoose treats the whole path as a StringA nested object uses the key type, so Mongoose reads it as a typeFix

What does the error look like?

  • CastError: Cast to string failed for value
  • Cast to string failed for value ... at path "items"

Cause 1 of 2

The array is declared as [String] but you push objects

1Check

Open the schema. A list of SchemaTypes such as [String] is a primitive array, and each element is cast to that type. An object cannot become a string, so the cast fails.

grep -n "items" models/Order.js

2Fix

Describe the element as a sub-schema, so the field becomes a document array that accepts objects with those keys.

const ItemSchema = new mongoose.Schema({ name: String, qty: Number });
const OrderSchema = new mongoose.Schema({
  items: [ItemSchema],   // document array
});

3Verify

Push an object and save. It should save with no CastError.

const order = await Order.create({});
order.items.push({ name: 'milk', qty: 2 });
await order.save();
console.log(order.items[0].name);

Confirmed by mongoosejs.com

Did this fix it?

Cause 2 of 2

A nested object uses the key type, so Mongoose reads it as a type

1Check

Mongoose treats type as a special property. In an object like { type: String, ticker: String } the path is read as one String, so pushing or setting an object fails to cast.

grep -n "type:" models/*.js

2Fix

Nest the real field named type inside its own type declaration, as the Mongoose docs show.

const holdingSchema = new mongoose.Schema({
  asset: {
    type: { type: String },
    ticker: String,
  },
});

3Verify

Create a document with the nested object and confirm it saves without a CastError.

await Holding.create({ asset: { type: 'stock', ticker: 'ABC' } });

Confirmed by mongoosejs.com

Did this fix it?

How do I stop it coming back?

  • Use a sub-schema for any array that holds objects; keep [String] and [Number] for plain values.
  • Avoid naming a field type. If you must, use the type: { type: String } form.
  • Validate req.body shape before pushing it into a document.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.