0

required format image

I want to object data into MongoDB using spring and I have hardcoded it. please how to write a schema for that and I have taken it as an example only. I have a different type of categories in it I have taken only clothes. please tell me how to write one schema for a different type of categories and query too.

please find the attachment for your reference

2 Answers 2

1

I would recommend going though Spring Data MongoDB documentation for specifics on mapping java objects to MongoDB documents. Your case would look similar to:

@Document
public class Clothes {

  @Id
  private ObjectId id;

  private Men men;
  private Women women;

  // getters & setters
}

You would need to define each sub class but this should be the gist of it.

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

Comments

0

What you can do is create a simple POJO (Plain Old Java Object) and with that you can insert that object into the data base. The the following example:

@Document
public class OAuthModel implements Serializable {

    @Id
    String username;

    @Indexed
    String oAuthID;

    @Indexed
    String type;
    
// Getter and Setters and Construct.
}

When I insert this object in the DB by calling:

OAuthModel authModel = new OAuthModel(username,firebaseToken.getUid(), OAuthHosts.GOOGLE.getType());
oAuthRepo.insert(authModel);

It will then be seen as this in the Database: enter image description here

Keep in mind this will work no matter what your object looks like, you can have hashmaps etc. The should be a built in serialization.

5 Comments

i am in the entry-level so could u please explain me and I required same like in the image format
Sure. So when we send data, in this case objects. We want to have want to convert that to a standerized form. In most cases that is a format called JSON. With the data in JSON format we can convert data to a JSON and vice versa. This is know as serialization. So with the @Document annotation we tell spring to generate the JSON automatically when we save data in our mongo database. No matter what type of data you have, arrays, double, ints, etc.
in other word have an object with all the data you need, that is the fields. add the @Document annotation above the class and then with your repo class do repo.insert(TheObjectYouWant)
tried with @Document saving empty data in the document. if possible pls tell me the schema
Im sorry Im a little lost. You need help with creating the data schema?

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.