Add element to grandchild collection in MongoDb - node.js

I have a MongoDb document like this
"schools":
[
"name" : "University",
"classes" :
[
{
"name":"Chem",
"teachers":
[
"Joe",
"Bill"
]
},
{
"name":"Math",
"teachers":
[
"Julie",
"Phil"
]
},
],
// More schools/classes/teachers here
]
How do I add a new teacher to the Math class?
(I'm writing this in node.js)

For the specific case you listed, you can do it like this:
myDocument.schools[0].classes[1].teachers.push("A new teacher");
myDocument.save();
For general cases (eg, add teacher to a class named "xyz"), you'd have to loop through the appropriate array(s) to find the item you're looking for.

Related

JHipster - How to generate Entity with a field as List of String?

I'm using jhipster-generator 4.14.5 and im trying to generate an Entity with a field Persons. But Persons is a List of String List<String> Persons.
How can i achieve it in JHipster. I tried to generate a simple field as String, then i changed the POJO like this :
#ElementCollection
#Column(name="persons")
List<String> persons;
The domain.json containing the whole table remain not touched.
I tried to run the application, after running liquibase:diff, without success. How can i fix it?
Use the generator entity to create a relationship :
Create an entity Person (maybe with only the "name", but more fields will soon be needed. Like "active", some dates ...)
.jhipster/[YourEntity].json should contain :
"fields": [
{
"fieldName": "xxx",
"fieldType": "Integer"
}
],
"relationships": [
{
"relationshipName": "person",
"otherEntityName": "person",
"relationshipType": "one-to-many",
"relationshipValidateRules": [
"required"
],
"otherEntityField": "name"
}
],
don't forget to commit before using the generator. Maybe you will need multiple executions to get it right.

MongoDb nested object selection [duplicate]

This question already has answers here:
Return only matched sub-document elements within a nested array
(3 answers)
Closed 5 years ago.
I have the multiple nested objects and lists, like below
{
"_id": "5a76be26ca96e22f08af2a19",
"testId": "123",
"testName": "summerTest",
"subjects": [
{
"subjectName": "Maths",
"testDetails": [
{
"testNumber": "0001",
"startTime": "2/18/18 13:30",
"endTime": "2/18/18 13:30",
"testDuriation": "01:00:00",
"questions": [
{...}
]
},
{
"testNumber": "0002",
"startTime": "2/18/18 13:30",
"endTime": "2/18/18 13:30",
"testDuriation": "01:00:00",
"questions": [
{...}
]
}
]
}
i want to select testNumber 0002 only. using mongoclient in my express js.
collection.find({ "testId": "123", "subjects.subjectName": "Maths", "subjects.testDetails.testNumber": "0002" }).toArray(function (err, data) {}..
But it will return entire TestId 123 document anyone help me. Thanks
Will be available
db.collection.aggregate([
{$unwind : '$subjects'},
{$project : {'_id': 0 , 'array' : '$subjects.testDetails'}},
{$unwind : '$array'},
{$match: {'array.testNumber' : '0002' }}
])
With a find you always return a whole document, so you need to add a projection to only show what you need.
By the way in your find filter there is an error, because if you want to filter only collections with a particular subjects.subjectName and subjects.testDetails.testNumber you need to use $elemMatch (https://docs.mongodb.com/manual/reference/operator/query/elemMatch/). If you don't do this it will return all document where in the subjects array there is one element with the first property and another one with the second property.

how to query nested array of objects in mongodb?

i am trying to query nested array of objects in mongodb from node js, tried all the solutions but no luck. can anyone please help this on priority?
I have tried following :
{
"name": "Science",
"chapters": [
{
"name": "ScienceChap1",
"tests": [
{
"name": "ScienceChap1Test1",
"id": 1,
"marks": 10,
"duration": 30,
"questions": [
{
"question": "What is the capital city of New Mexico?",
"type": "mcq",
"choice": [
"Guadalajara",
"Albuquerque",
"Santa Fe",
"Taos"
],
"answer": [
"Santa Fe",
"Taos"
]
},
{
"question": "Who is the author of beowulf?",
"type": "notmcq",
"choice": [
"Mark Twain",
"Shakespeare",
"Abraham Lincoln",
"Newton"
],
"answer": [
"Shakespeare"
]
}
]
},
{
"name": "ScienceChap1test2",
"id": 2,
"marks": 20,
"duration": 30,
"questions": [
{
"question": "What is the capital city of New Mexico?",
"type": "mcq",
"choice": [
"Guadalajara",
"Albuquerque",
"Santa Fe",
"Taos"
],
"answer": [
"Santa Fe",
"Taos"
]
},
{
"question": "Who is the author of beowulf?",
"type": "notmcq",
"choice": [
"Mark Twain",
"Shakespeare",
"Abraham Lincoln",
"Newton"
],
"answer": [
"Shakespeare"
]
}
]
}
]
}
]
}
Here is what I've tried so far but still can't get it to work
db.quiz.find({name:"Science"},{"tests":0,chapters:{$elemMatch:{name:"ScienceCh‌​ap1"}}})
db.quiz.find({ chapters: { $elemMatch: {$elemMatch: { name:"ScienceChap1Test1" } } }})
db.quiz.find({name:"Science"},{chapters:{$elemMatch:{$elemMatch:{name:"Scienc‌​eChap1Test1"}}}}) ({ name:"Science"},{ chapters: { $elemMatch: {$elemMatch: { name:"ScienceChap1Test1" } } }})
Aggregation Framework
You can use the aggregation framework to transform and combine documents in a collection to display to the client. You build a pipeline that processes a stream of documents through several building blocks: filtering, projecting, grouping, sorting, etc.
If you want get the mcq type questions from the test named "ScienceChap1Test1", you would do the following:
db.quiz.aggregate(
//Match the documents by query. Search for science course
{"$match":{"name":"Science"}},
//De-normalize the nested array of chapters.
{"$unwind":"$chapters"},
{"$unwind":"$chapters.tests"},
//Match the document with test name Science Chapter
{"$match":{"chapters.tests.name":"ScienceChap1test2"}},
//Unwind nested questions array
{"$unwind":"$chapters.tests.questions"},
//Match questions of type mcq
{"$match":{"chapters.tests.questions.type":"mcq"}}
).pretty()
The result will be:
{
"_id" : ObjectId("5629eb252e95c020d4a0c5a5"),
"name" : "Science",
"chapters" : {
"name" : "ScienceChap1",
"tests" : {
"name" : "ScienceChap1test2",
"id" : 2,
"marks" : 20,
"duration" : 30,
"questions" : {
"question" : "What is the capital city of New Mexico?",
"type" : "mcq",
"choice" : [
"Guadalajara",
"Albuquerque",
"Santa Fe",
"Taos"
],
"answer" : [
"Santa Fe",
"Taos"
]
}
}
}
}
$elemMatch doesn't work for sub documents. You can use the aggregation framework for "array filtering" by using $unwind.
You can delete each line from the bottom of each command in the aggregation pipeline in the above code to observe the pipelines behavior.
You should try the following queries in the mongodb simple javascript shell.
There could be Two Scenarios.
Scenario One
If you simply want to return the documents that contain certain chapter names or test names for example just one argument in find will do.
For the find method the document you want to be returned is specified by the first argument. You could return documents with the name Science by doing this:
db.quiz.find({name:"Science"})
You could specify criteria to match a single embedded document in an array by using $elemMatch. To find a document that has a chapter with the name ScienceChap1. You could do this:
db.quiz.find({"chapters":{"$elemMatch":{"name":"ScienceChap1"}}})
If you wanted your criteria to be a test name then you could use the dot operator like this:
db.quiz.find({"chapters.tests":{"$elemMatch":{"name":"ScienceChap1Test1"}}})
Scenario Two - Specifying Which Keys to Return
If you want to specify which keys to Return you can pass a second argument to find (or findOne) specifying the keys you want. In your case you can search for the document name and then provide which keys to return like so.
db.quiz.find({name:"Science"},{"chapters":1})
//Would return
{
"_id": ObjectId(...),
"chapters": [
"name": "ScienceChap2",
"tests: [..all object content here..]
}
If you only want to return the marks from each test object you can use the dot operator to do so:
db.quiz.find({name:"Science"},{"chapters.tests.marks":1})
//Would return
{
"_id": ObjectId(...),
"chapters": [
"tests: [
{"marks":10},
{"marks":20}
]
}
If you only want to return the questions from each test:
db.quiz.find({name:"Science"},{"chapters.tests.questions":1})
Test these out. I hope these help.

How to use NEIGHBORS in AQL?

I'm new to ArangoDB and a growing fan already. Among many things we need to translate many-to-many relations into graphs, and query efficiently in there.
However I can't seem to reproduce the behaviour in NEIGHBORS as described in the cookbook
under "Using Edge Collections".
After I insert data and run:
FOR b IN books RETURN { book: b, authors: NEIGHBORS(books, written, b._id, 'inbound') }
[
{
"book" : {
"_id" : "books/10519631898915",
"_key" : "10519631898915",
"_rev" : "10519631898915",
"title" : "The beauty of JOINS"
},
"authors" : [ ]
}
]
Empty authors list! I tried this instead:
FOR b IN books RETURN { book: b, authors: NEIGHBORS(authors, written, b._id, 'inbound') }
[
{
"book" : {
"_id" : "books/10519631898915",
"_key" : "10519631898915",
"_rev" : "10519631898915",
"title" : "The beauty of JOINS"
},
"authors" : [
"authors/10519474612515",
"authors/10519475792163"
]
}
]
Which returns the _id list. None of those return what I need as in the cookbook, which is the expected edge/vertex structure.
(All has been tested in 2.6.9)
How is the use of NEIGHBORS intended and how do I get to my goal in pure AQL?
Is there a standard documentation of NEIGHBORS (and other graph AQL features) somewhere with description and type of each argument as well as return value?
Have you tried the includeData option for NEIGHBORS?
FOR b IN books RETURN { book: b, authors: NEIGHBORS(authors, written, b._id, 'inbound', [], {includeData: true}) }
That worked in my test.
It will be way more performant then PATHS on large datasets (PATHS computes much more irrelevant information)
Note: The empty array [] is used to define edges that should be followed only. With an empty array we follow all edges, but you could also follow special edges f.e. {label: "written"} instead of [].
Right, I found one solution:
FOR p IN PATHS(books, written, 'inbound')
RETURN p.destination
Result:
Warnings:
[1577], 'collection 'books' used as expression operand'
Result:
[
{
"_id": "books/10519631898915",
"_rev": "10519631898915",
"_key": "10519631898915",
"title": "The beauty of JOINS"
},
{
"_id": "authors/10519474612515",
"_rev": "10519474612515",
"_key": "10519474612515",
"name": {
"first": "John",
"last": "Doe"
}
},
{
"_id": "authors/10519475792163",
"_rev": "10519475792163",
"_key": "10519475792163",
"name": {
"first": "Maxima",
"last": "Musterfrau"
}
}
]
It gets the destination vertices at least, but it doesn't seem right since I get a warning and the source vertex is included as a destination.
Further elaboration and suggestions are very welcome.
UPDATE (2017): NEIGHBORS is no longer supported in AQL 3.x
Instead of
NEIGHBORS(books, written, b._id, 'inbound')
you could write a sub-query:
(FOR v IN 1..1 INBOUND b written RETURN v)

How to do mongoose aggregation with nested array documents

I have a Mongodb collection, Polls with following schema
{
"options" : [
{
"_id" : Object Id,
"option" : String,
"votes" : [ Object Id ] // object ids of users who voted
},.....
]
}
Assume i have userId of the user in node js to whom I want to send this info.
My task is to
(1) include an extra field in the above json object (which i get using mongoose).
as
"myVote" : option._id
I need to find option._id for which
options[someIndex].votes contains userId
(2) change the existing "votes" field in each option to represent number of votes on a particular option as can be seen in example
Example:
{
"options" : [
{
"_id" : 1,
"option" : "A",
"votes" : [ 1,2,3 ]
},
{
"_id" : 2,
"option" : "B",
"votes" : [ 5 ]
},
{
"_id" : 3,
"option" : "C",
"votes" : [ ]
}
]
}
So if i user with user id = 5 wants to see the poll, then i need to send following info:
Expected Result :
{
"my_vote" : 2, // user with id 5 voted on option with id 2
"options" : [
{
"_id" : 1,
"option" : "A",
"votes" : 3 //num of votes on option "A"
},
{
"_id" : 2,
"option" : "B",
"votes" : 1 //num of votes on option "B"
},
{
"_id" : 3,
"option" : "C",
"votes" : 0 //num of votes on option "C"
}
]
}
Since it was the question that you actually asked that was neither really provided in the current acceptance answer, and also that it does some unnecessary things, there is another approach:
var userId = 5; // A variable to work into the submitted pipeline
db.sample.aggregate([
{ "$unwind": "$options" },
{ "$group": {
"_id": "$_id",
"my_vote": { "$min": {
"$cond": [
{ "$setIsSubset": [ [userId], "$options.votes" ] },
"$options._id",
false
]
}},
"options": { "$push": {
"_id": "$options._id",
"option": "$options.option",
"votes": { "$size": "$options.votes" }
}}
}}
])
Which of course will give you output per document like this:
{
"_id" : ObjectId("5573a0a8b67e246aba2b4b6e"),
"my_vote" : 2,
"options" : [
{
"_id" : 1,
"option" : "A",
"votes" : 3
},
{
"_id" : 2,
"option" : "B",
"votes" : 1
},
{
"_id" : 3,
"option" : "C",
"votes" : 0
}
]
}
So what you are doing here is using $unwind in order to break down the array for inspection first. The following $group stage ( and the only other stage you need ) makes use of the $min and $push operators for re-construction.
Inside each of those operations, the $cond operation tests the array content via $setIsSubset and either returns the matched _id value or false. When reconstructing the inner array element, specify all elements rather than just the top level document in arguments to $push and make use of the $size operator to count the elements in the array.
You also make mention with a link to another question about dealing with an empty array with $unwind. The $size operator here will do the right thing, so it is not required to $unwind and project a "dummy" value where the array is empty in this case.
Grand note, unless you are actually "aggregating" across documents it generally would be advised to do this operation in client code rather than the aggregation framework. Using $unwind effectively creates a new document in the aggregation pipeline for each element of the array contained in each document, which produces significant overhead.
For such an operation acting on distinct documents only, client code is more efficient to process each document individually.
If you really must persist that server processing is the way to do this, then this is probably most efficient using $map instead:
db.sample.aggregate([
{ "$project": {
"my_vote": {
"$setDifference": [
{ "$map": {
"input": "$options",
"as": "o",
"in": { "$cond": [
{ "$setIsSubset": [ [userId], "$$o.votes" ] },
"$$o._id",
false
]}
}},
[false]
]
},
"options": { "$map": {
"input": "$options",
"as": "o",
"in": {
"_id": "$$o._id",
"option": "$$o.option",
"votes": { "$size": "$$o.votes" }
}
}}
}}
])
So this just "projects" the re-worked results for each document. The my_vote is not the same though, since it is a single element array ( or possible multiple matches ) that the aggregation framework lacks the operators to reduce to a non array element without further overhead:
{
"_id" : ObjectId("5573a0a8b67e246aba2b4b6e"),
"options" : [
{
"_id" : 1,
"option" : "A",
"votes" : 3
},
{
"_id" : 2,
"option" : "B",
"votes" : 1
},
{
"_id" : 3,
"option" : "C",
"votes" : 0
}
],
"my_vote" : [
2
]
}
Check out this question.
It's not asking the same thing, but there's no way to do what you're asking without multiple queries anyway. I would modify the JSON you get back directly, as you're just displaying extra info that is already contained in the result of the query.
Save the userID you're querying for.
Take the results of your query (options array in an object), search through the votes of each element in the array.
When you've found the right vote, attach the _id (perhaps add 'n/a' if you don't find a vote).
Write a function that does 2 and 3, and you can just pass it a userID, and get back a new object with myVote attached.
I don't think doing it like this will be slower than doing another query in Mongoose.

Resources