Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  191] [ 4]  / answers: 1 / hits: 16724  / 12 Years ago, thu, november 29, 2012, 12:00:00

My mongoDB collection looks like this :



{
_id : ObjectId(5070310e0f3350482b00011d),
emails : [
{
_id : ObjectId(5070310e0f3350482b000120),
_type : Email,
name : work,
email : [email protected],
current : true
}
]
}


and this is the .js code i use to print the contents :



c = db.contacts.findOne( { emails.email : { $ne : null } }, { emails : 1 } )

print(c._id.toString() + + c.emails[0]);


when I try to run this javascript file, it is just displaying the id but not the email array.



output:
5070310e0f3350482b00011d [object bson_object]


but when I try c.emails[0].email is is giving proper result. i.e. [email protected]



All I need is I want to display the whole emails embedded object.



i.e.
emails : [
{
_id : ObjectId(5070310e0f3350482b000120),
_type : Email,
name : work,
email : [email protected],
current : true
}
]


Where I am going wrong?. Any help would be appreciated.


More From » mongodb

 Answers
31

You need printjson to output a nicely formatted JSON:



printjson(c.emails[0]);


Here it is the documentation.


[#81725] Wednesday, November 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;