0

I'm trying to print the values of an nested array.But getting execute script error.How do I print object BSON and avoid error in for nested array. Note : I want to do with print and not find().

Customer schema

 {
 "name" : "Sam",   
 "phone" : [ 
 {
   "home" : "123456",
  "work" : "045842"
 }]}

query

db.getCollection('customer').find({}).forEach( function(cust) 
{
print("Customer Name : " + cust.name); // prints Sam
print("Home Contact : " + cust.phone) // prints [object BSON]
print("Home Contact : " + cust.phone.home) // throws error
});

2 Answers 2

1

You could use aggregation if there are multiple items in the array

 db.collectionName.aggregate([

    { $unwind: { path: "$phone", preserveNullAndEmptyArrays: true}},

 ]).forEach(function(doc){

    print(doc.name)
    if(doc.phone !== undefined) print(doc.phone.home)
    if(doc.phone !== undefined) print(doc.phone.work)
 })
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Can you explain what is the actual purpose of aggregate and what does unwind here mean { $unwind: { path: "$phone", preserveNullAndEmptyArrays: true}},
Aggregate takes in a series of stages that can slice and dice the data for analysis. $unwind is a one of the many stages provided by the MongoDB aggregation framework. Check out this blog about aggregation 101 - blog.appriver.com/dev/…
1

You just need to convert the object to string and access the array;

   print("Home Contact : " + JSON.stringify(cust.phone[0])) 
    // prints ` Home Contact: { "home" : "123456", "work" : "045842" }
   print("Home Contact : " + cust.phone[0].home) // "123456"

An example:

aireclaimRs:PRIMARY> use test
switched to db test
aireclaimRs:PRIMARY> db.createCollection('customer')
{ "ok" : 1 }
aireclaimRs:PRIMARY> db.customer.insert( {
...  "name" : "Sam",   
...  "phone" : [ 
...  {
...    "home" : "123456",
...   "work" : "045842"
...  }]})
WriteResult({ "nInserted" : 1 })
aireclaimRs:PRIMARY> db.getCollection('customer').find().forEach(function(cust){
... print("Customer Name : " + cust.name);
... print("Homes Contact : " + JSON.stringify(cust.phone[0]));
... print("Home Contact : " + cust.phone[0].home)
... })
Customer Name : Sam
Homes Contact : {"home":"123456","work":"045842"}
Home Contact : 123456

3 Comments

Thanks for the answer. I tried them. print("Home Contact : " + JSON.stringify(cust.phone[0])) and print("Home Contact : " + cust.phone[0].home) is not working. Only this works.... print("Home Contact : " + JSON.stringify(cust.phone))
They really SHOULD work, both of them: I've edited the answer
Thanks. I had some records with empty phone array (basically data issue). So i added a if condition like if(cust.phone){.....code} , and then your solution worked. Thanks once again.

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.