MongoDB sharing user data best practices - database

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 hour ago.
Improve this question
What is the best way to access nested user data as another user with MongoDB?
I have not been able to find any info on what the best practices are for being able to access user data from another user.
Given that MongoDB is document based storage I would assume that this is the best way to store data that is coupled to a user. My issue is that I am not sure what the best way would be to allow users 2 and 3 to access the list that is owned by user 1.
{
"_id": 1,
"username": "johndoe",
"hashed_password": "xxxxx",
"lists": [
{
"id": 1,
"name": "list 1",
"owner_id": 1,
"shared": true,
"shared_users": [2, 3],
"items": [
{
"name": "item 1",
"description": "item description"
},
{
"name": "item 2",
"description": "item description"
}
]
},
{
"id": 1,
"name": "different list",
"owner_id": 1,
"shared": false,
"shared_users": [],
"items": [
{
"name": "item 1",
"description": "item description"
},
{
"name": "item 2",
"description": "item description"
}
]
}
]
}
I was thinking that separating the users and the lists would be the best way around this as then each user can have a reference to each list as either the owner or a shared viewer. But I understand this is not an ideal solution.
{
"_id": 1,
"type": "USER",
"username": "johndoe",
"hashed_password": "xxxxx",
"owned_lists": [{ "mapped_id": 5 }]
},
{
"_id": 5,
"type": "LIST",
"name": "list 1",
"owner_id": 1,
"shared": true,
"shared_users": [2, 3],
"items": [
{
"name": "item 1",
"description": "item description"
},
{
"name": "item 2",
"description": "item description"
}
]
}
I found this question that goes through some of the considerations from an app usage (read/write) perspective but not really much into accessing nested user data.
Should I then have 2 collections for Lists and Users or have them all in the same collection?
What would be the best way to achieve this?

Related

Array inside Object inside Array - method map don't want work - Cannot read properties of undefined

i'm a new in React. I downloaded the api request and need length array comments and other properties, but React all time msg to me: Cannot read properties of undefined (reading 'length') or Cannot read properties of undefined (reading 'map').
Where is a problem ?
My array from api is:
const myArrayApi = [
{
"id": 1,
"title": "Add tags for solutions",
"category": "enhancement",
"upvotes": 112,
"status": "suggestion",
"description": "Easier to search for solutions based on a specific stack.",
"comments": [
{
"id": 1,
"content": "Awesome idea! Trying to find framework-specific projects within the hubs can be tedious",
"user": {
"image": "./assets/user-images/image-suzanne.jpg",
"name": "Suzanne Chang",
"username": "upbeat1811"
}
},
{
"id": 2,
"content": "Please use fun, color-coded labels to easily identify them at a glance",
"user": {
"image": "./assets/user-images/image-thomas.jpg",
"name": "Thomas Hood",
"username": "brawnybrave"
}
}
]
},
{
"id": 2,
"title": "Add a dark theme option",
"category": "feature",
"upvotes": 99,
"status": "suggestion",
"description": "It would help people with light sensitivities and who prefer dark mode.",
"comments": [
{
"id": 3,
"content": "Also, please allow styles to be applied based on system preferences. I would love to be able to browse Frontend Mentor in the evening after my device’s dark mode turns on without the bright background it currently has.",
"user": {
"image": "./assets/user-images/image-elijah.jpg",
"name": "Elijah Moss",
"username": "hexagon.bestagon"
}
},
{
"id": 4,
"content": "Second this! I do a lot of late night coding and reading. Adding a dark theme can be great for preventing eye strain and the headaches that result. It’s also quite a trend with modern apps and apparently saves battery life.",
"user": {
"image": "./assets/user-images/image-james.jpg",
"name": "James Skinner",
"username": "hummingbird1"
},
"replies": [
{
"content": "While waiting for dark mode, there are browser extensions that will also do the job. Search for 'dark theme' followed by your browser. There might be a need to turn off the extension for sites with naturally black backgrounds though.",
"replyingTo": "hummingbird1",
"user": {
"image": "./assets/user-images/image-anne.jpg",
"name": "Anne Valentine",
"username": "annev1990"
}
},
{
"content": "Good point! Using any kind of style extension is great and can be highly customizable, like the ability to change contrast and brightness. I'd prefer not to use one of such extensions, however, for security and privacy reasons.",
"replyingTo": "annev1990",
"user": {
"image": "./assets/user-images/image-ryan.jpg",
"name": "Ryan Welles",
"username": "voyager.344"
}
}
]
}
]
},
{
"id": 3,
"title": "Q&A within the challenge hubs",
"category": "feature",
"upvotes": 65,
"status": "suggestion",
"description": "Challenge-specific Q&A would make for easy reference.",
"comments": [
{
"id": 5,
"content": "Much easier to get answers from devs who can relate, since they've either finished the challenge themselves or are in the middle of it.",
"user": {
"image": "./assets/user-images/image-george.jpg",
"name": "George Partridge",
"username": "soccerviewer8"
}
}
]
},
{
"id": 4,
"title": "Add image/video upload to feedback",
"category": "enhancement",
"upvotes": 51,
"status": "suggestion",
"description": "Images and screencasts can enhance comments on solutions.",
"comments": [
{
"id": 6,
"content": "Right now, there is no ability to add images while giving feedback which isn't ideal because I have to use another app to show what I mean",
"user": {
"image": "./assets/user-images/image-javier.jpg",
"name": "Javier Pollard",
"username": "warlikeduke"
}
},
{
"id": 7,
"content": "Yes I'd like to see this as well. Sometimes I want to add a short video or gif to explain the site's behavior..",
"user": {
"image": "./assets/user-images/image-roxanne.jpg",
"name": "Roxanne Travis",
"username": "peppersprime32"
}
}
]
},
{
"id": 5,
"title": "Ability to follow others",
"category": "feature",
"upvotes": 42,
"status": "suggestion",
"description": "Stay updated on comments and solutions other people post.",
"comments": [
{
"id": 8,
"content": "I also want to be notified when devs I follow submit projects on FEM. Is in-app notification also in the pipeline?",
"user": {
"image": "./assets/user-images/image-victoria.jpg",
"name": "Victoria Mejia",
"username": "arlen_the_marlin"
},
"replies": [
{
"content": "Bumping this. It would be good to have a tab with a feed of people I follow so it's easy to see what challenges they’ve done lately. I learn a lot by reading good developers' code.",
"replyingTo": "arlen_the_marlin",
"user": {
"image": "./assets/user-images/image-zena.jpg",
"name": "Zena Kelley",
"username": "velvetround"
}
}
]
},
{
"id": 9,
"content": "I've been saving the profile URLs of a few people and I check what they’ve been doing from time to time. Being able to follow them solves that",
"user": {
"image": "./assets/user-images/image-jackson.jpg",
"name": "Jackson Barker",
"username": "countryspirit"
}
}
]
},
{
"id": 6,
"title": "Preview images not loading",
"category": "bug",
"upvotes": 3,
"status": "suggestion",
"description": "Challenge preview images are missing when you apply a filter."
},
{
"id": 7,
"title": "More comprehensive reports",
"category": "feature",
"upvotes": 123,
"status": "planned",
"description": "It would be great to see a more detailed breakdown of solutions.",
"comments": [
{
"id": 10,
"content": "This would be awesome! It would be so helpful to see an overview of my code in a way that makes it easy to spot where things could be improved.",
"user": {
"image": "./assets/user-images/image-victoria.jpg",
"name": "Victoria Mejia",
"username": "arlen_the_marlin"
}
},
{
"id": 11,
"content": "Yeah, this would be really good. I'd love to see deeper insights into my code!",
"user": {
"image": "./assets/user-images/image-jackson.jpg",
"name": "Jackson Barker",
"username": "countryspirit"
}
}
]
},
{
"id": 8,
"title": "Learning paths",
"category": "feature",
"upvotes": 28,
"status": "planned",
"description": "Sequenced projects for different goals to help people improve.",
"comments": [
{
"id": 12,
"content": "Having a path through the challenges that I could follow would be brilliant! Sometimes I'm not sure which challenge would be the best next step to take. So this would help me navigate through them!",
"user": {
"image": "./assets/user-images/image-george.jpg",
"name": "George Partridge",
"username": "soccerviewer8"
}
}
]
},
{
"id": 9,
"title": "One-click portfolio generation",
"category": "feature",
"upvotes": 62,
"status": "in-progress",
"description": "Add ability to create professional looking portfolio from profile.",
"comments": [
{
"id": 13,
"content": "I haven't built a portfolio site yet, so this would be really helpful. Might it also be possible to choose layout and colour themes?!",
"user": {
"image": "./assets/user-images/image-ryan.jpg",
"name": "Ryan Welles",
"username": "voyager.344"
}
}
]
},
{
"id": 10,
"title": "Bookmark challenges",
"category": "feature",
"upvotes": 31,
"status": "in-progress",
"description": "Be able to bookmark challenges to take later on.",
"comments": [
{
"id": 14,
"content": "This would be great! At the moment, I'm just starting challenges in order to save them. But this means the My Challenges section is overflowing with projects and is hard to manage. Being able to bookmark challenges would be really helpful.",
"user": {
"image": "./assets/user-images/image-suzanne.jpg",
"name": "Suzanne Chang",
"username": "upbeat1811"
}
}
]
},
{
"id": 11,
"title": "Animated solution screenshots",
"category": "bug",
"upvotes": 9,
"status": "in-progress",
"description": "Screenshots of solutions with animations don’t display correctly."
},
{
"id": 12,
"title": "Add micro-interactions",
"category": "enhancement",
"upvotes": 71,
"status": "live",
"description": "Small animations at specific points can add delight.",
"comments": [
{
"id": 15,
"content": "I'd love to see this! It always makes me so happy to see little details like these on websites.",
"user": {
"image": "./assets/user-images/image-victoria.jpg",
"name": "Victoria Mejia",
"username": "arlen_the_marlin"
},
"replies": [
{
"content": "Me too! I'd also love to see celebrations at specific points as well. It would help people take a moment to celebrate their achievements!",
"replyingTo": "arlen_the_marlin",
"user": {
"image": "./assets/user-images/image-suzanne.jpg",
"name": "Suzanne Chang",
"username": "upbeat1811"
}
}
]
}
]
}
]
const MyArrayFromApi = () => {
const [arrayApi, setArrayApi] = useState(myArrayApi);
return (
<div>
{arrayApi.map((elementApi => {
return (
<div key={elementApi.id}>
<div>
<h1>{elementApi.title}</h1>
<p>{elementApi.category}</p>
<p>{elementApi.upvotes}</p>
<p>{elementApi.description}</p>
</div>
{elementApi.comments.map((elementComments => {
return (
<div key={elementComments.id}><p>{elementComments.content}</p></div>
)
}))}
</div>
)
}))}
</div>
)
}
export { MyArrayFromApi }
enter image description here
The error happened because of "elementApi.comments.map".
There are some items that don't have "comments" property. e.g. id = 6, 11
So please change "elementApi.comments.map" to "elementApi.comments?.map".
Incorrect use of parentheses with arrow function arguments. Use them like the below sample code. Or you can ignore them if you have only one argument: arrayApi.map(elementApi => {
//....rest of the code
{arrayApi.map((elementApi) => {
return (
//...
)
})}
</div>
//...
Same issue with elementApi.comments.map(...
Also with elementsApi.comments, in some objects the comments are undefined. So in order to deal with this you need to call this attribute with the optional chaining operator (?.)
Ex: elementApi.comments?.map(...
From MDN
The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null
here is the solution to this https://stackblitz.com/edit/react-cdwvfz?file=src/App.js
I have used The optional chaining operator (?.) (comments?). this operator enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.
like in the myArrayApi array, some objects did not have comment key so after using this operator, the undefined error would not appear
const [arrayApi, setArrayApi] = useState(myArrayApi);
return (
<div>
{arrayApi.map((elementApi) => {
return (
<div key={elementApi.id}>
<div>
<h1>{elementApi.title}</h1>
<p>{elementApi.category}</p>
<p>{elementApi.upvotes}</p>
<p>{elementApi.description}</p>
</div>
{elementApi?.comments?.map((elementComments) => {
return (
<div key={elementComments.id}>
<p>{elementComments.content}</p>
</div>
);
})}
</div>
);
})}
</div>
);

How to say if an entity is a property or a resource?

We cannot decide what is the best solution.
We have a collection of call centers containing several properties:
{
"blabla": 0.0,
"_id": "543d2a1f2c6d7a0e89f2eb77",
"balbla_foo": "543d2a1f2c6d7a0e89f2eab2",
"blabla_bar": {},
peek: {
a: "boo",
},
bla: {
bla: "+33891111111",
},
}
We have to add groups which are in a tree.
"groups": {
"1": {
"label": "Branch 1",
"children": ["2", "3"]
},
"2": {
"label": "Branch 2",
"children": []
},
"3": {
"label": "Branch 3",
"children": []
}
}
We debated where should it be. So, some think it is a property of the call center because it is a one-to-one relation.
Others think it is a resource so it has to be separated from the call center.
What is the best structure?
Thanks for your help.
EDIT:
What is the most coherent structure ?

How do I read a nested structure from solr?

I am lost and need your advice. I have a nested Solr Document containing multiple levels of sub-documents. Here is a JSON example so you can see the full structure:
{
"id": "Test Library",
"description": "example of nested document",
"content_type": "library",
"authors": [{
"id": "author1",
"content_type": "author",
"name": "First Author",
"books": [{
"id": "book1",
"content_type": "book",
"title": "title of book 1"
}],
"shortStories": [{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
}]
},
{
"id": "author2",
"content_type": "author",
"name": "Second Author",
"books": [{
"id": "book1",
"content_type": "book",
"title": "title of book 1"
}],
"shortStories": [{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
}]
}]
}
I want to query for a document and retrieve the nested structure. I tried using the ChildDocumentTranformerFactory but it flattened the result to be just Library and all other documents as children:
{
"id": "Test Library",
"description": "example of nested document",
"content_type": "library",
"_childDocuments_":[
{"id": "author1",
"content_type": "author",
"name": "First Author"
},
{"id": "book1",
"content_type": "book",
"title": "title of book 1"
},
{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
},
{
"id": "author2",
"content_type": "author",
"name": "Second Author"
},
{
"id": "book1",
"content_type": "book",
"title": "title of book 1"
},
{
"id": "shortStory1",
"content_type": "shortStory",
"title": "title of short story 1"
}
]
}
Here are the query parameters I used:
q={!parent which='content_type:library'}
df=id
fl=*,[child parentFilter='content_type:library' childFilter='id:*']
wt=json
indent=true
What is the best way to read the nested structure from Solr? Do I need to do some sort of faceting?
I am using Solr version 5.2.1
Unfortunately solr does not support that right now.
ChildDocTransformerFactory returns all descendant documents of each parent document matching your query in a flat list nested inside the matching parent document
Not sure if there is a work around for this.
Please look into Block Join to virtually relate the parent and child.
https://github.com/lucidworks/solrj-nested-docs

Background images not appearing in cards anymore

My app was functioning fine until sometime last week. For some reason all the background images stopped appearing in the actual Google Glass. They do appear in the playgorund. Anyone know how to fix this? Here's an example of my app home card (I changed the URLs). The home.jpg image does not appear.
{
"kind": "mirror#timelineItem",
"id": "a48a9a6a-67df-4035-859f-b33f05c4e11e",
"created": "2013-06-14T16:24:17.460Z",
"updated": "2013-06-14T16:24:17.460Z",
"etag": "\"r3ghbVW9Rp1kDP4UexS05_pFx4E/gCTDXyXBhsaPhUc6MnVaYblah\"",
"html": "<article class=\"photo\">\t<img src=\"https://myapp.appspot.com/static/images/home.jpg?cacheKiller=098234092384\" width=\"100%\" height=\"100%\">\t<div class=\"photo-overlay\"></div>\t<section>\t\t<p class=\"text-auto-size\"></p>\t</section></article>",
"menuItems": [
{
"id": "menu1",
"action": "CUSTOM",
"values": [
{
"displayName": "Menu 1",
"iconUrl": "https://myapp.appspot.com/static/images/icon.png?cacheKiller=098234092384"
}
]
},
{
"id": "menu2",
"action": "CUSTOM",
"values": [
{
"displayName": "Menu 2",
"iconUrl": "https://myapp.appspot.com/static/images/icon.png?cacheKiller=098234092384"
}
]
},
{
"id": "menu3",
"action": "CUSTOM",
"values": [
{
"displayName": "Menu 3",
"iconUrl": "https://myapp.appspot.com/static/images/icon.png?cacheKiller=098234092384"
}
]
},
{
"id": "menu4",
"action": "CUSTOM",
"values": [
{
"displayName": "Menu 4",
"iconUrl": "https://myapp.appspot.com/static/images/icon.png?cacheKiller=098234092384"
}
]
},
{
"id": "pinned",
"action": "TOGGLE_PINNED"
}
],
"notification": {
"level": "DEFAULT"
}
}
This appears to be a more widespread problem, possibly since the introduction of XE6. In many of the cases, the background image will show up later.
Since you have some concrete examples of this, I suggest you open a bug in the issue tracker.
An update was made and issue corrected. Just need to soft reset (hold down for 15 seconds) and be connected to data.
The issue was css related for photos. It did not effect videos.

How can i handle this scenario

I am using Backbone.js, unexpectedly i am in the confused position now, because of this scenario..
my test object :
test: {
"links": {
"nameLink": {
"name": "name 1",
"name": "name 2",
"name": "name 3"
},
"ageLink": {
"age": "1",
"age": "2",
"age": "3"
}
},
"data": {
"element": {
"project": "project 1",
"title": "title1",
"brand": "brand1"
},
"element": {
"project": "project 2",
"title": "title2",
"brand": "brand2"
},
"element": {
"project": "project 3",
"title": "title3",
"brand": "brand3"
}
}
}
I do have the josn data from server, it has the project name, links(there is nested links under), time stamps.
how can i split the data, and append appropriate UL elements. ( by name, by age ect.)
On click on the name, age links, how can sort (by name, age) the datas i appended to body tag? - without sorting the links menu?
How can i keep all this updated?
any good suggestion please?

Resources