1

This is my json object

{
"account_id" :  "1",
"sections" : [
    "name" : "sec1",
    "label" : {
        "label1" : "text1",
        "label2" : "text2"
        }
    },
    "name" : "sec2",
    "label" : {
        "label3" : "text3",
        "label4" : "text4",
        "label5" : "text5"
         }
    },       
]
}

So in this json I wanted to query the label object where sector= sec1. I have used the below code but it didn't work.

var getData = (db, query) => { 
  return db
    .collection(TABLE_NAME)
    .find(query, { account_id: { sections: { label: 1 } } })
    .toArrayAsync();
};

var dataList = (db, event) => {
  let dataQuery = {
     account_id: id,
     'sections.name': event.params.section
   };
  return getData(db, dataQuery);
};

module.exports.getData = (event, cb) => {
  return using(connectDatabase(), db => {
    return dataList (db, event);
   }).then(data => cb(null, responseObj(data, 200)), err => 
cb(responseObj(err, 500)));
};

Could someone kindly help me? Thanks inadvance.

2
  • You Json doesn't seem to be valid. Could you double check it you correctly copy/pasted it? Commented Feb 28, 2019 at 11:45
  • Now the json object was updated Commented Feb 28, 2019 at 11:58

2 Answers 2

1

Try something like this. use $project, we can selectively remove or retain field and we can reassign existing field values and derive entirely new values. after projecting the labels and name do a $match to extract the document by name. One thing to notice is that by using $project,it will automatically assign the document's _id.

    var dataList = (db, event) => {
        return db
            .collection(TABLE_NAME)
            .aggregate([ 
              { 
                $match: { account_id: your_id } 
              },
              {
                $unwind: '$sections'
               },
              {
                $project:{labels:'$sections.label',name:'$sections.name'}
              },
               {
               $match:{name:section_name}
             }]).toArray();
          };
Sign up to request clarification or add additional context in comments.

Comments

0

You have to use aggregate method with $unwind syntax to find item in array of object.

var dataList = (db, event) => {
    return db
        .collection(TABLE_NAME)
        .aggregate([
            {
                $match: {
                    account_id: id,
                }
            },
            { $unwind: "$comments" },
            {
                $match: {
                    'name': event.params.section
                }
            }
        ])
        .toArrayAsync();
};

Result:

[{
    "account_id": "1",
    "sections": {
        "name": "sec2",
        "label": {
            "label3": "text3",
            "label4": "text4",
            "label5": "text5"
        }
    }
}]

3 Comments

No it didn't work, the response is a null object . Do I need to do any changes in the .find method
.find(query, { account_id: { sections: { label: 1 } } }) => .find(query)
When I changed the code as you have mentioned I got object arrays of both sec1 and sec2. But I wanted the label object which belongs to one particular section. Could you kindly explain how I can do the task?

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.