Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJonathan Grieve
Treehouse Moderator 91,253 PointsUnable to update Mongo Collections
When running the command to find the author
> db.users.find({},{_id: true})
{ "_id" : ObjectId("56e99247d8ad7fe5ecc642d2") }
{ "_id" : ObjectId("56e99247d8ad7fe5ecc642d3") }
{ "_id" : ObjectId("56e99247d8ad7fe5ecc642d4") }
{ "_id" : ObjectId("56e99247d8ad7fe5ecc642d5") }
{ "_id" : ObjectId("56e99247d8ad7fe5ecc642d6") }
{ "_id" : ObjectId("56e99247d8ad7fe5ecc642d7") }
So far so good.
But then I find myself unable to get the query returned. no matter which collection I try.
> db.posts.find({author:ObjectId("56e99247d8ad7fe5ecc642d4")})
> db.users.find({author:ObjectId("56e99247d8ad7fe5ecc642d4")})
>
I get no results returned.
Also I follow through the process and the returned below
> db.posts.update({author: ObjectId("56e99247d8ad7fe5ecc642d4")}, {$set:{tags: [
'foo', 'bar', 'interesting'], title: "I'm an updated title!"}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
>
Any idea's what's going on? The records are not updated after I check by querying the database.
Thanks.
2 Answers
Shae Ellis
8,151 PointsI think I discovered the problem. I also ran into the same thing, then it dawned on me... there are 6 authors, but only 4 posts. And 2 posts have the same author. You may want to check for matching _id fields so you know that the author id you're choosing is valid in a post.
Once I picked a particular author id to look for, and ran this:
db.posts.find({author: ObjectId("56ec2bb27130988e7cf2c80d")});
I received an actual post, because that id matched a user id. I hope that helps!
(Sorry for all the edits... it seems the markdown language isn't working for me so that I can show the query as a snippet. Weird)
Jonathan Grieve
Treehouse Moderator 91,253 PointsI see where you're going with that explanation and it would make sense as to why I'm not picking up any results in a query.
I'm positvie that I tried more than one object id for my query but I'll go again and see what I can find :)
Shae Ellis
8,151 PointsThis is all new to me... I'm a bit more savvy with PostgreSQL and MySQL, so when I couldn't pull up a post from a randomly chosen author I was all "what gives??". I actually went back and did a find (a couple times) for the posts and then looked at the author ObjectIds trying to match them up. I know what you're talking about.... after I managed to do that, I couldn't get the update to work. Ends up I forgot my colon after $set. ;)
So many reasons that JavaScript-related stuff frustrates the crap outta me.... it's all the $(func{},(func{func({});}){}); and such. LOL
Jonathan Grieve
Treehouse Moderator 91,253 PointsIt worked. :)
Hurrah, which can only mean you were correct. I managed to get the matching message from the video and I then queried the collection to check the data had been changed.
Hurrah! I'm still trying to get my head around Mongo though. I think i prefer SQlite/My SQL but I'm liking Mongo so far :D
Modou Sawo
13,141 PointsSide notes for anyone who may run into updating the records, make sure you put author in quotes.
So that will be for example:
db.posts.update({"author": ObjectId("591f6a6fc9da9d74b48e13f7")}, {$set: {tags: ['foo', 'bar', 'interesting'], title: "I'm an Updated Title!"}})
Shae Ellis
8,151 PointsShae Ellis
8,151 PointsI'm so glad you got it figured out! :D And I'm in complete agreement with you about relational dbs. I'm also just more familiar with themβMongo is a different beast, but I am loving the more-intuitive queries... and no JOIN statements! Hooray! Hahaha