1

Say User2 has these objects of data, they are stored in User2.objects as an array of objects.

User2:{objects:[{object1},{object2},{object3}]} 

If anyone other then User2 needs to query this data, like User1 needs any of those objects that pertain to them. Then it should be broken out into it's own collections of in MongoDB db right?

Because say a User1 wants to find every object like that are part of. They would need to have a reference to all the User2s they created data with then look through every object in each user, and return the one they need.

I should break those objects out into their own collections? Then I can just index user ids and each user can just query once for their own id.

Sorry if this Q is confusing I'm a little lost.

Clifford
  • 88,407
  • 13
  • 85
  • 165
fancy
  • 48,619
  • 62
  • 153
  • 231

1 Answers1

5

It appears as though there may be some confusion between Mongo Document structure and using authentication with MongoDB.

The documentation on how to set up user authentication for a Mongo Database is here: http://www.mongodb.org/display/DOCS/Security+and+Authentication

If User2 needs to run a query on a collection that was created by User1, then User2 must have an account with the Database where that collection resides, and must be properly authenticated.

The example document provided is also a little confusing. It is a better idea to use key names that will be the same across all documents. For example:

{userName:"user1", name:"Marc"},
{userName:"user2", name:"Jeff"},
{userName:"user3", name:"Steve"}

is preferable to

{user1:"Marc"},
{user2:"Jeff"},
{user3:"Steve"}

In the second example, the username (user1, user2, etc) will have to be known in order to find out the name of the user. MongoDB does not support wildcards in queries.

The following document structure would be preferable:

{
user: "User2", 
objects:[object1,object2,object3]
},
{
user: "User1", 
objects:[object1,object2,object3]
}

All of the objects created by user1 could be retrieved with the following query:

> db.<your collection name>.find({user: "User1"}, {objects:1})

For more information on the MongoDB document structure, I recommend reading the following:

http://www.mongodb.org/display/DOCS/Schema+Design - A great introduction to the way data is stored in MongoDB, including example documents, best practices, and an introduction to indexing.

Hopefully the above will put you on the right track in terms of deciding on a schema for your collection and creating users and setting permissions. Authentication is one of MongoDB's more advanced features, so I would begin by focusing on building an efficient schema and organizing your data correctly before worrying about authentication.

If you have any additional questions about these topics, or anything else MongoDB-related, the Community is here to help! Good Luck!

Marc
  • 5,488
  • 29
  • 18