Simon
Simon

Reputation: 19948

Creating a mongodb entry for my schema using the java driver

The below displays a "profile" object recorded in mongodb. I know this type of schema is very possible on mongoDB where you would create an inner object called "name" and "alias" inside your profile object. I have done it myself and tested this already.

{"name": {"name": "Peter", "show": false},
 "alias": {"alias":"GoofyDuck", "show": false}}

I feel that "name" and "alias" inner objects are not really necessary as I have an inner object called alias with a field called alias and would like my "profile" object to look like this instead -Is this even possible on mongoDB? Please show me with code if it is.

{{"name": "Peter", "show": false},
  {"alias":"GoofyDuck", "show": false}}

I also know that this is possible but don't really want to embed it into an array:

{[{"name": "Peter", "show": false},
  {"alias":"GoofyDuck", "show": false}]}

Upvotes: 0

Views: 471

Answers (1)

Anything that is valid JSON is a valid document in Mongo. Since the middle code block is not valid JSON (the inner objects are missing keys) it is not valid for Mongo. On the same note the 3rd code block is also not valid JSON: the array is missing a key.

I think that the schema design you are looking for may be:

{
    name: "...",
    showName: true/false,
    alias: "...",
    showAlias: true/false
}

Upvotes: 1

Related Questions