Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB MapReduce. $exists on nested field

I want to execute map-reduce operation on specific documents from MongoDB.

Let's say, we have this "document":

{
  a: {
    b: {
      c:{}
    }
  }
}

I want to select only those documents, that have an a.b.c field. From this documentation page I know, that $exists operator can help.

To check, that a exists I should use:

query: {
  a: { $exists : true }
}

But what query should I use for nested field, described before?

query: {
  //?
}
like image 954
UnknownJoe Avatar asked Dec 29 '14 16:12

UnknownJoe


People also ask

How do I query a nested field in MongoDB?

Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.

How do I delete a nested object in MongoDB?

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator. Now field "UserZipCode": "20010" has been removed from a doubly-nested array.

How do I get only certain fields in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

What is the preferred method of querying embedded documents in MongoDB?

Use the $elemMatch operator to query embedded documents. Use conditional operators to query embedded documents. Use Visual Query Builder to query embedded documents.


1 Answers

This isn't related to the fact of using map-reduce.

query: {
  'a.b.c' : { $exists : true }
}

So is a standard query on an embedded document: http://docs.mongodb.org/manual/tutorial/query-documents/#embedded-documents

like image 108
David Mabodo Avatar answered Oct 13 '22 22:10

David Mabodo