0

I am building a digital-card generator app in node.js. In this I want to store the card data as JSON in the MongoDB database. I am using mongoose ODM. but I think mongoose does not support the data-type of Json or object.

The card schema:

const digitalCardSchema = new mongoose.Schema(
  {
    userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    data: {
      type: String,
    },
  },
  {
    timestamps: true,
  }
);

How can I do that?

3
  • MongoDB stores data as documents. Documents have a JSON like structure, and also has additional data types like Date, ObjectId, etc. Commented Aug 19, 2022 at 11:09
  • I am aware of that but I want specifically data field in the above schema as JSON Commented Aug 19, 2022 at 11:13
  • 1
    You can store it as an object with properties and values like in a JSON. Commented Aug 19, 2022 at 11:38

1 Answer 1

1

you can try this option - there is a Mixed schema type

https://mongoosejs.com/docs/schematypes.html#mixed

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.