0

I am attempting to save a JSON string to mongoDB, it seems that the length of the string is too great and I get the following error message.

MongoError: WiredTigerIndex::insert: key too large to index, failing  23012

My schema is fairly simple,

const metadataSchema = new Schema({
  userId: { type: String, unique: false },
  metadataJSON: { type: String, unique: false, failIndexKeyTooLong: false }
});
const saveMetadata = async (userId, customMetadata) => {
  let metadata = METADATA({
    userId: userId,
    metadataJSON: JSON.stringify(customMetadata)
  });
  metadata.save(err => {
    if (err)
      console.log(
        "ERROR - Metadata was not inserted and here is the reason : " + err
      );
    else console.log("All is good, metadata was inserted");
  });
};

Any suggestions on how I could insert this large string? Thanks in advance.

Stennie
  • 63,885
  • 14
  • 149
  • 175
BreadMAN
  • 1
  • 1
  • Possible duplicate of [Cannot create index in mongodb, "key too large to index"](https://stackoverflow.com/questions/27792706/cannot-create-index-in-mongodb-key-too-large-to-index) – Mickael B. Sep 01 '19 at 21:49
  • The `failIndexKeyTooLong` parameter is a server configuration option (not per-index) and is not recommended for correctness. This option was created for migration from pre-2.6 MongoDB deployments which would insert a document but skip indexing where the length of the value was >1024 bytes. Since neither field in your schema appear to have an index declared at the moment, I suspect you may have defined one previously. Try checking for indexes via the `mongo` shell with `db.metadata.getIndexes()` (or whatever your collection is called). You probably need to drop an index on `metadataJSON`. – Stennie Sep 02 '19 at 03:14

0 Answers0