still unimpressed with NoSQL
Aug. 15th, 2020 10:46 amPhi's a smart guy, so I'm willing to entertain the notion that he put forth that a NoSQL would've been the Right Tool (TM) to implement Facebook. I don't have time to re-write Facebook. But, so, for my class project— which is just supposed to be a tiny little toy program— I'm trying to implement just a teeny, tiny little bit of Facebook-like functionality. Just really basic stuff. Like, for example, you should be able to comment on comments, and comment on those comments on comments, etc., recursively, to a pretty arbitrary depth. (I don't use Facebook so I wouldn't know if Facebook doesn't actually do this. I do know that you can have seemingly arbitrary depths of comment nesting in Dreamwidth/Livejournal, and in whatever Captain Awkward uses for her columns— WordPress? Seems like pretty basic functionality.)
Maybe this is the wrong NoSQL database, but I'm using MongoDB (with mongoose on top) because that's what was taught in the class. Taught in a very shallow way. Maybe MongoDB is a toy, but it seems very popular. Like, people say that they are doing real projects with it?
Here's what I just posted to the class discussion forum:
Surely I'm missing something. There must be some function I'm not calling or something I'm not setting somewhere. But in Googling this one would think I was asking for how to do something really advanced and esoteric. What??? Surely it's not the case that most projects out there in MongoDB have all the complexity of an address book. So, surely, this is something that many people have had to do... Why do I feel like I'm bushwhacking a new path into the wilderness? Why can't I find the wide well-trodden path? What kind of genius does it take to do something more complex than an address book in the NoSQL? In contrast, with SQL, with just a few simple easy to understand keywords, you can put together data schema of any complexity.
Maybe this is the wrong NoSQL database, but I'm using MongoDB (with mongoose on top) because that's what was taught in the class. Taught in a very shallow way. Maybe MongoDB is a toy, but it seems very popular. Like, people say that they are doing real projects with it?
Here's what I just posted to the class discussion forum:
I am getting frustrated with MongoDB trying to put embedded documents into embedded documents.
Here's the structure of my data: A Group can have many blog posts. Each blog post can have many comments. In turn each comment can have comments, which can have comments... This part of the schema, at least, is very hierarchical, so I'm trying to do it with embedded documents. Here's the relevant parts of the schema, n.b. that I use the same data structure for blog posts and comments ("Post"):let groupSchema = new Schema({ name: String, nickname: String, banner: String, threads: [postSchema], }) const Group = mongoose.model("Group", groupSchema); let postSchema = new Schema() postSchema.add({ title: String, bodytext: String, comments: [postSchema], photos: [String], isPublic: Boolean, isPublished: Boolean }) const Post = mongoose.model("Post", postSchema);
Here's a function which should add a comment to a post and then a comment to the comment:exports.addCommentsToPost = async (gnick, id) => { let group = await Group.findOne({nickname:gnick}) let post = group.threads.find(element => element._id == id) let comment = new Post({ title: "this is a comment", bodytext: "here is the body of the comment", isPublic: true, isPublished: true, comments:[ ] }) post.comments.push(comment) await group.save() console.log("ID for comment:" + comment._id) let metaComment = new Post({ title: "meta-comment", bodytext: "And I have a comment on your commnt.", isPublic: true, isPublished: true, comments: [] }) comment.comments.push(metaComment) await group.save() console.log("ID for comment on comment:" + metaComment._id) }
When I run this, I get something like this on stdout:
ID for comment:5f37f1e41bfca6268d65dbae
ID for comment on comment:5f37f1e51bfca6268d65dbb0
This suggests that mongodb "saw", in some sense, not just the comment but the comment on the comment? It did give them both _id fields.
However, when I find the top-level post in the database, here's what gets returned:{ photos: [], comments: [ { photos: [], comments: [], _id: 5f37f1e41bfca6268d65dbae, title: 'this is a comment', bodytext: 'here is the body of the comment', isPublic: true, isPublished: true } ], _id: 5f37f1d11bfca6268d65dbac, isPublished: true, bodytext: 'This is the text of my blog entry.', isPublic: true, title: 'what I did on Saturday' }
The comment on the top-level post is there. But the comment on the comment is gone. The comment's comments value is an empty array.
What am I missing?
Surely I'm missing something. There must be some function I'm not calling or something I'm not setting somewhere. But in Googling this one would think I was asking for how to do something really advanced and esoteric. What??? Surely it's not the case that most projects out there in MongoDB have all the complexity of an address book. So, surely, this is something that many people have had to do... Why do I feel like I'm bushwhacking a new path into the wilderness? Why can't I find the wide well-trodden path? What kind of genius does it take to do something more complex than an address book in the NoSQL? In contrast, with SQL, with just a few simple easy to understand keywords, you can put together data schema of any complexity.