0

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}]}
4
  • What is the question here? How to reorganize your schema? Yes, that is very possible. Commented Nov 26, 2015 at 20:02
  • No - is it possible to create the schema I want (the schema in the middle) on mongoDB? If so, please show me with code. I don't see how it is possible because you can either create an inner object like how I have shown in the first example, or you can put the inner DBObjects into an array like I shown in the third example. Commented Nov 26, 2015 at 20:05
  • 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. Commented Nov 26, 2015 at 20:22
  • Agreed. I think this is the correct answer. Thanks! I will just have to change my schema to a valid json then which is most likely going to be the embedded inner objects as per my first example. The reason for my question is because I do not want to duplicate my field name and object name (you can see 'alias' appears twice in my first eg) but I guess it has to be done like that to create the valid json. Commented Nov 26, 2015 at 20:28

1 Answer 1

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
}
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.