1

Is it possible to use Dot Notation when dealing with nested documents?

http://www.mongodb.org/display/DOCS/Dot+Notation+(Reaching+into+Objects)

I'm trying to query the results of a map/reduce and therefore need to run a query like this:

find({'_id.page' : 'ThisPage', '_id.user' : 'AUser'}) 

Trying this in Node code returns no rows but the same query works as expected in mongodb shell.

2
  • Are you able to profile this and see what query actually gets sent to the server from Node? Commented Feb 8, 2011 at 2:02
  • Yes. It seems it's a problem in the mongodb driver, it drops the quotation marks of from the keys so the query turns in to find({_id.page:'ThisPage', _id.user:'AUser'}) Commented Feb 8, 2011 at 9:06

2 Answers 2

2

Dot notation isn't required for reaching inside of documents for queries, you can use document notation instead.

find({'_id.page' : 'ThisPage', '_id.user' : 'AUser'})

could instead be

find({_id: {page: 'ThisPage', user: 'AUser'}})

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

Comments

0

It is very possible, I've done it before.

Why do you have nested documents under your _id property? Not sure what your use case is but that seems a bit strange. _id is a special property that is always the unique id of the document. So it might be getting treated special by the driver (i.e. doesn't expect there to be sub documents). Maybe try putting your sub-documents under a different property name.

2 Comments

the result of a mapreduce can well have multi-part id. moreover, why does it matter if it is called _id or foo? the question is about problem accessing a property that is buried more than one level deep. can you elaborate about "how" you've done it before?
I simply followed the documentation and use the quoted dot notation. For example 'foo.bar' should work. I've never done this in NodeJS, but have done it in other languages. And yes I think you're right, there's no restriction in MongoDB itself as to emitting multi-part _id properties. What I meant in my original answer is that the language drivers sometimes try to be smart and apply magical stuff. For example in the C# world it always assumes that the _id property of a document is a ObjectId and not a mult-part document. So my suggestion was to simply try renaming _id to something else.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.