0

I have following in my accounts collection:

   { "_id" : ObjectId("4fc55125476e0a27d9000003"), 
"created_at" : ISODate("2012-05-29T22:43:49Z"), 
"teachers" : [  {   
"_id" : ObjectId("4fc55125476e0a27d9000004"),   
"updated_at" : ISODate("2012-05-29T22:43:49Z"),     
"created_at" : ISODate("2012-05-29T22:43:49Z") 
} ], 
"updated_at" : ISODate("2012-05-29T22:43:49Z") 
}

I want to query for an account that has a teacher with _id 4fc55125476e0a27d9000003.

If I use the command

db.accounts.findOne({"teachers._id" : ObjectId("4fc55125476e0a27d9000004")})

it returns null.

Also, why does the mongo command line thing hang when I use find instead of findOne with the above command.

3
  • I just saved your exact document and queried it via your exact query and got back the document. Is the document exactly as you have it pasted when you just do db.accounts.find()? Commented May 30, 2012 at 5:17
  • Not sure if this is just a typo but you write once that you want to query for 4fc55125476e0a27d9000003, but your query shows a 4 at the end. If your dataset doesn't contain an entry with ...3 you won't find it. Your _id which holds the ...3 is the one containing those teacher information in it. The query should work anyway. Commented May 30, 2012 at 6:39
  • @philnate You are right. I meant to use the teacher's id i.e. the one with 4 in it. Maybe that is why I was getting a null. I will try this again (later this evening, can't right now) and post what I find. Thanks! Commented May 30, 2012 at 17:57

1 Answer 1

1

As others pointed out, the query actually works. Not sure what was going on the other day that I couldn't get it to return the correct result. Maybe I was using an incorrect db. Thanks for the help and apologies for wasting your time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.