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 trial

Databases Mongo Basics Go Further With Mongo Updating Data

Using find() returns nothing with ObjectID

I've used db.posts.find({}, {_id: true}) and it fetches 4 records.

db.posts.find({}, {_id: true})
{
  "_id": ObjectId("5b05471cbb36b738a23ed101")
}
{
  "_id": ObjectId("5b05471cbb36b738a23ed102")
}
{
  "_id": ObjectId("5b05471cbb36b738a23ed103")
}
{
  "_id": ObjectId("5b05471cbb36b738a23ed104")
}

When I copy one and type in

db.posts.find({author: ObjectId("5b05471cbb36b738a23ed104")})

however, I get the result of 'fetched 0 record(s) in 1ms' I can see the records, they're right there! Why isn't this method returning anything? What am I doing wrong here? I'm at a loss...

1 Answer

Adam Beer
Adam Beer
11,314 Points

If your first step is "db.users.find({}, {_id: true})" and the second step "db.posts.find({author: ObjectId("5b05471cbb36b738a23ed104")})" then now what happends?

Yes, that works! I didn't realise I needed to use 'users.find()' instead of 'posts.find()' in the first step! Thank you for helping :)

Adam Beer
Adam Beer
11,314 Points

Your welcome! Happy coding!