0

In MongoDB I have the following the document.

{
"_id": {
    "$oid": "58402903734d1d19c9a34cdd"
},
"deliveryComp": "abc",
"poorDel": 0,
"okDel": 1,
"wellDel": 0,
"veryWellDel": 1
}

When I call this data from mongodb using an angular controller it is being returned as an array, like so:

[{
"_id": {
    "$oid": "58402903734d1d19c9a34cdd"
},
"deliveryComp": "abc",
"poorDel": 0,
"okDel": 1,
"wellDel": 0,
"veryWellDel": 1
}]

I want to display the values but I don't want to use ng-repeat. Each value needs to be displayed in different parts of the app. There is no "repeating" data.

How can I achieve this?

7
  • do you know which entry you want to display at which position? Commented Dec 22, 2016 at 9:55
  • What's the size of the array? Commented Dec 22, 2016 at 9:55
  • Extract each object into its own variable and then link that to the element on the site? (Hope I understood what you wanted) Commented Dec 22, 2016 at 9:55
  • Why can't you use ng-repeat? Commented Dec 22, 2016 at 9:56
  • @Karthik the size of the array is 1. Exactly the same as shown in the question. It will always have only one object. I didn't save it as an array in the database. But its being returned as an array. Commented Dec 22, 2016 at 9:57

2 Answers 2

1

If you've only got 1 value in the array, you can use;

{{mongoDbData[0]._id.$oid}}

For every value in your array, use 0 because it's the first item in your array.

Sign up to request clarification or add additional context in comments.

Comments

0

If not mistaken, the issue seems to be that the document object you were expecting in wrapped as the only element of an array. In your controller, do as follows:

// assuming document to be the array-wrapped response
$scope.documentData = document[0];

so that in your view you can use data binding on documentData

<span>{{ documentData._id.$oid }}</span>

This will remove the need to always fetch the first element of the array in your data binding expression.

Comments

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.