AngularFire & ngOptions - angularjs

I am struggling to populate a select form element. Here is the syntax that I have so far. It will not grab the program.title and populate the ngOptions.
<select ng-model="student.program" ng-options="program.title for program in programList.title">
<option value="">Select A Program</option>
</select>
Here is the basic JSON structure of my data on Firebase:
{
"-JNIBeRDAuNN9c7SGZ91" : {
"location" : "Online",
"director" : "Shawn Clark",
"title" : "Web Design & Development"
},
"-JNIBuaQlWyEyWRjONPa" : {
"location" : "Online",
"director" : "Walen Morrow",
"title" : "Graphic Design"
},
"-JNIBmIwasMYOPRPupHl" : {
"location" : "Campus",
"director" : "Tracy Monohan",
"title" : "Web Design & Development"
},
"-JNKGPNj-lGzqHJ92RTD" : {
"location" : "Online",
"director" : "Glen Speedy",
"title" : "Theory of Animation & Physics"
},
"-JNK9oTO97PfSo-0wQfY" : {
"location" : "Campus",
"director" : "John Myers",
"title" : "Instructional Design & Technology"
},
"-JNIC3Tldo-Qi3ru2ph7" : {
"location" : "Campus",
"director" : "George Narrow",
"title" : "Graphic Design"
},
"-JNK8cdgdHKjH2meKl9x" : {
"location" : "Online",
"director" : "Stacy McNally",
"title" : "Instructional Design & Technology"
}
}

I believe you are looking for
<select ng-model="student.program" ng-options="program.title for (id, program) in programList"><option value="">Select A Program</option></select>
Your JSON's root object is an object, not an array. See angular documentation for object data sources
See Plunker

Try the following code.
It should work:
<select ng-model="student.program" ng-options="program.title for program in programList"><option value="">Select A Program</option></select>
Thanks

Related

Find only the documents which have two embedded documents

I'm using Mongodb to analysee a Nobel prizes dataset which documents look like these:
> db.laureate.find().pretty().limit(1)
{
"_id" : ObjectId("604bc8c847d640142f02b3b1"),
"id" : "1",
"firstname" : "Wilhelm Conrad",
"surname" : "Röntgen",
"born" : "1845-03-27",
"died" : "1923-02-10",
"bornCountry" : "Prussia (now Germany)",
"bornCountryCode" : "DE",
"bornCity" : "Lennep (now Remscheid)",
"diedCountry" : "Germany",
"diedCountryCode" : "DE",
"diedCity" : "Munich",
"gender" : "male",
"prizes" : [
{
"year" : "1901",
"category" : "physics",
"share" : "1",
"motivation" : "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\"",
"affiliations" : [
{
"name" : "Munich University",
"city" : "Munich",
"country" : "Germany"
}
]
}
]
}
As you see the column "prizes" has embedded documents and the query I am trying to do is finding only those laureates who won two prizes (which I already know to be Marie Curie and Linus Pauling) can you help me with that?
Thanks in advance!
The $size operator should work fine for this. You could read about it if you want in this link: https://docs.mongodb.com/manual/reference/operator/query/size/
Your new query:
db.laureate.find({prizes: {$size: 2}}).pretty().limit(1)

MongoDB Transform element fields into array of values only

I need help with following thing in MongoDB, what I'm trying to do is to get only marks in reviews into it's own array of values only.
Code i got so far:
db.lodging.aggregate([
{$project:{
reviews:"$host.reviews",host:"$host"
}
},
{$unwind: "$reviews"},
])
JSON Example:
"host" : {
"name" : "Grimes",
"surname" : "Terrell",
"gender" : "male",
"age" : NumberInt(55),
"picture" : "https://api.adorable.io/avatars/285/GrimesTerrell.png",
"reviews" : [
{
"reviewer" : "Mae Ryan",
"date" : "2015-06-01T02:41:46 -02:00",
"helpful" : NumberInt(8),
"kind" : NumberInt(1),
"responsive" : NumberInt(5)
},
{
"reviewer" : "Nixon Johnson",
"date" : "2016-02-08T10:35:12 -01:00",
"helpful" : NumberInt(1),
"kind" : NumberInt(1),
"responsive" : NumberInt(9)
},
]
}
This is what im trying to achieve:
{
"host" : {
"name" : "Grimes",
"surname" : "Terrell",
"gender" : "male",
"age" : NumberInt(55),
"picture" : "https://api.adorable.io/avatars/285/GrimesTerrell.png",
"reviews" : [
{
"marks" : [8,1,5],
},
{
"marks" : [1,1,9],
},
]
}
Assuming your marks' field names are always helpful, kind, responsive, you can use $map.
Here is a mongo playground for you reference

MongoDB - Array element as variable

Lets say here is the same document:
{
"_id" : 1.0,
"custname" : "metlife",
"address" : {
"city" : "Bangalore",
"country" : "INDIA"
}
}
And if I want to push an extra field to this document, something like below:
db.customers.updateMany(
{"address.country":"INDIA"},
{$push : {city: "$address.country"}}
)
It results in wrong update:
{
"_id" : 1.0,
"custname" : "metlife",
"address" : {
"city" : "Bangalore",
"country" : "INDIA"
},
"city" : "$address.city"
}
Instead of this:
{
"_id" : 1.0,
"custname" : "metlife",
"address" : {
"city" : "Bangalore",
"country" : "INDIA"
},
"city" : "Bangalore"
}
How do I achieve the above result?
You can't refer to other field values in update currently (more here). There is a workaround in aggregation framework (using $out) but it will replace entire collection.
I think that you can consider using $rename in your case. It will not add new field but it can move city to the top level of your document.
db.customers.updateMany({"address.country":"INDIA"}, {$rename: {"address.city": "city"}})
will give you following structure:
{ "_id" : 1, "custname" : "metlife", "address" : { "country" : "INDIA" }, "city" : "Bangalore" }
like #mickl said : You can't refer to other field values in update currently,
you have to iterate through your collection to update the documents, try this :
db.eval(function() {
db.collection.find({"address.country":"INDIA"}).forEach(function(e) {
e.city= e.address.city;
db.collection.save(e);
});
});
Keep in mind that this will block the DB until all updates are done.
try this
db.customers.updateMany(
{"address.country":"INDIA"},
{$push : {city: "address.country"}}
)
remove $ sign

how to push a comment in an embedded document in mongodb with java driver

I have the below document:
enter code here
{
"_id" : ObjectId("56c49b52a5b24ba2a979a964"),
"FirstName" : "satvik",
"LastName" : "ponakala",
"Major" : "Information Assurance",
"RoomPost" : [
{
"postId" : 1,
"title" : "I have a room to share at Garden Square",
"comments" : [
{
"comment1" : " i want to join",
"author" : "ashish"
},
{
"comment2" : " already booked",
"author" : "puneeth"
}
]
},
{
"postId" : 2,
"title" : "I have a room to share at Ansley Falls",
"comments" : [
{
"comment1" : " How many can stay",
"author" : "sandeep"
},
{
"comment2" : "can i come there next sunday??",
"author" : "sandeep"
}
]
}
]
}
How to add a new comment {
"comment3":"push comment from java",
"author":"Java"
} in the comments section under the post ID : 1 from java
you meed to match postId 1 and then push a new comment to "comments" , something like this :
db.[your collection name].update({"RoomPost" :{$elemMatch :{"postId" : 1}}}, {$push : {"RoomPost.$.comments": {"comment3" :"some comment"}}})
writing it with the java driver is easy, the 'update' section is something like that :
BasicDBObject update = new BasicDBObject("$push", new BasicDBObject("RoomPost.$.comments",new BasicDBObject("comment3" ,"some comment" ) ))
Hope it helps.

Mongodb - How to update a nested Array

I have to update an Array nested in another Array.
Is it possible to do that with the $addtoset operator?
Here is my document.
I have a library wich has an Array of authors who have an Array of Books.
In this exemple, i'd like to add a new category to the book id 1 of the author id 1
How can i do that ?
{
"id" : NumberLong(666),
"library" : [
{
"id" : NumberLong(8888),
"author" : [
{
"id" : NumberLong(1),
"books" : [
{
"id":1,
"title" : "plop",
"category" : ["horror"],
"isbn" : 12345
},
{
"id":2,
"title" : "plup",
"category" : ["comics"],
"isbn" : 6789
}
]
},
{
"id" : NumberLong(2),
"books" : [
{
"id":3,
"title" : "blop",
"category" : ["horror"],
"isbn" : 96325
},
{
"id":4,
"title" : "blup",
"category" : ["comics"],
"isbn" : 74125
}
]
}
]
}
]
}
I tried this :
db.library.update(
{"id":666,"author.id":1,"books.id":1},
{"$addToSet": {"author.$.books.category": "humour" }}
)
But it doesn't works
How does it works ?
thanks a lot
db.library.update(
{"id":666,"library.author.id":1,"library.author.books.id":1},
{"$addToSet": {"library.0.author.0.books.$.category": "Book"}}
)

Resources