Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases Mongo Basics Go Further With Mongo Updating Data

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,065 Points

Unable 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 })
>

Mongo screen

Any idea's what's going on? The records are not updated after I check by querying the database.

Thanks.

Shae Ellis
Shae Ellis
8,151 Points

I'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

2 Answers

Shae Ellis
Shae Ellis
8,151 Points

I 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
Jonathan Grieve
Treehouse Moderator 91,065 Points

I 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
Shae Ellis
8,151 Points

This 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
Jonathan Grieve
Treehouse Moderator 91,065 Points

It 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
Modou Sawo
13,141 Points

Side 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!"}})