1

(I am an extreme newbie with extremely simple question):

If my database (from the documentation samples) consists of:

{ "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }
{ "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }
{ "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

I issue this command:

db.inventory.find( { item: /^j/ }, { size: 1 } )

I get back this document:

{
    "_id" : ObjectId("59448e8544c2d6b6ced4ac66"),
    "size" : {
        "h" : 14.0,
        "w" : 21.0,
        "uom" : "cm"
    }
}

What I want to do is, only get back "h". If I try this command:

db.inventory.find( { item: /^j/ }, { size.h: 1 } )

I get "Unexpected token ."

How can I cause only the "h" field of array "size" to return?

Thanks!

PS, I tried the example and answer in how to retrieve partial objects from object array in a field in mongodb, but the result was "Script executed successfully, but there are no results to show."

2
  • Because it's not an array. And the very specific error message should have pointed you to the "dot" as the clear source of the problem. Look at "Dot Notation" in the core documentation and the very clear example "<embedded document>.<field>" which very clearly shows the "quotes" "" Commented Jun 17, 2017 at 9:02
  • Thanks Neil. You are a great teacher. Commented Jun 18, 2017 at 15:10

2 Answers 2

2

You have to enclose the key with quotes so it's correct syntax. A key can only contain $, _, and alphanumeric characters. If the key is invalid inside the object literal, just enclose the key with quotes to bypass this:

db.inventory.find( { item: /^j/ }, { 'size.h': 1 } )
Sign up to request clarification or add additional context in comments.

Comments

0
//use when you want `_id,size` (As object) with its only property `h` in 
//$project,

db.inventory.aggregate([{$match:{  item: /^j/  } },{ $project:{ 
"size.h":"$size.h" } }])

//use when you want `_id,size` size ase whole object  
//$project,

db.inventory.aggregate([{$match:{  item: /^j/  } },{ $project:{ "size":1 } 
}]) 
  • You can use Aggregation which can help you to get data in any form .

  • It includes Pipes.

  • It accepts array

Note :- To ignore any property just change 1 to 0 in $project properties

Thanks

1 Comment

The other answer is exactly what I need for now. However, I really appreciate your answer and will study it because I am sure I will learn something I really need to know. Thanks very much!

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.