Update in embedded document(Nested collection) - javascript

I have a collection like User having list user and that user have list of user. Like hierarchy.
{
"_id" : ObjectId("55530326bc687d21783fd1ff"),
"Name" : "User 1",
"Role" : "Manager",
number:NumberLong(0),
"1" :
[
{
"_id" : ObjectId("55530326bc687d21783fd1fd"),
"Name" : "User 2",
"Role" : "Ass Manager",
number:NumberLong(0),
"1" :
[
.......
]
}
{
"_id" : ObjectId("55530326bc687d21783fd1fq"),
"Name" : "User 2",
"Role" : "Ass Manager",
number:NumberLong(1),
"1" :
[
.........
]
},
{
"_id" : ObjectId("55530326bc687d21783fd1fg"),
"Name" : "User 3",
"Role" : "Ass Manager",
number:NumberLong(2),
"1" :
[
........
]
}
],
"2" :
[
{
"_id" : ObjectId("55530326bc687d21783fd1fw"),
"Name" : "User 4",
"Role" : "Specialist",
number:NumberLong(0),
"1" :
[
.......
]
}
{
"_id" : ObjectId("55530326bc687d21783fd1fe"),
"Name" : "User 5",
"Role" : "Specialist",
number:NumberLong(1),
"1" :
[
.........
]
},
{
"_id" : ObjectId("55530326bc687d21783fd1fr"),
"Name" : "User 6",
"Role" : "Specialist",
number:NumberLong(2),
"1" :
[
........
]
}
]
}
The above is just one collection for sample, like this i have nearly 10000 document. I need to find the collection which have 'number' as 0. Even if any one embedded document have 'number' as 0 I want that document.
Note : I can't tell how many child will come for a user.

All right, I will assume that each of your User documents can have two arrays of children users (namely "1" and "2"), and that you have a maximum nesting level, say 3 (This means that a nested user cannot have more than 2 anchestors). By the way, the maximum nesting level allowed by mongodb is 100.
Probably this is not want you wanted: in this case you have issues with your schema design, because
An arbitrarily nested schema is not good
If keys are intended as indexes, you should consider using an array instead of an object (click here for a similar case).
Now, let's pretend that my assumptions were ok for you. Try (I'm calling your collection users since we don't usually capitalize collection names):
db.users.find({$or:["1.number" : 0, "2.number" : 0, "1.1.number":0, "1.2.number":0, ..., "2.2.1.number":0, "2.2.2.number":0]})
I have skipped some combinations that you need to add. Notice that you don't need to worry about the position in arrays, and that with only 3 levels of nesting there are already quite many clauses for the $or operator, and that will possibly convince you that it's better to follow the linked best practices.
NOTE for future readers: the OP has clarified in a comment that he doesn't actually need an update, but a find query.

Related

Mongoose reference collection or nested document 2 level deep

I'm using mongoose on node js and I'm having a Page collection such as
{
"_id" : ObjectId("5b3cf0e7ee00450156711a47"),
"language" : "en",
"published" : true,
"content" : [
{
"title" : "my title 1",
"subTitle" : "subtitle 1",
"items" : [
{
"title" : "my item title 1",
"subTitle" : "item subtitle 1",
},
{
"title" : "my item title 2",
"subTitle" : "item subtitle 2",
}
]
},
{
"title" : "my title 2",
"subTitle" : "subtitle 2",
}
],
"createdAt" : ISODate("2018-07-04T16:08:07.057Z"),
"__v" : 0
}
The "content" array includes many objects and some of those object include an "items" array that includes many object too. (nested on 2 level deep)
I was wondering if it wouldn't be better to reference content object instead of having them nested, because they're having nested documents too (inside items) so it's a 2 level deep nesting.
I have to use page.content.id(listId).items.id(id) in order to find them and page.content.id(listId).items.id(id)[key] = value; page.save(); to update them.
Considering there shouldn't have more than 3 "items" and 5 "content" maximum, what do you think would be the best solution, updating nested on 2 level deep document or content having a reference/collection?
I also plan to do a versioning which would influence this decision, as updating could/would create a new version of the document.
I would prefer reference collection here
Page Schema
{
"language" : String,
"published" : Boolean,
"content" : [
{type: mongoose.Schema.Types.ObjectId, ref: 'content'}
],
"createdAt" : Date,
}
Content Schema
{
"title" : String,
"subTitle" : String,
"items": [{type: mongoose.Schema.Types.ObjectId, ref: 'items'}]
}
Items Schema
{
"title" :String,
"subTitle" : String,
}

Compare mongoose ObjectId in Node js [duplicate]

I have two collections. The first collection contains students:
{ "_id" : ObjectId("51780f796ec4051a536015cf"), "name" : "John" }
{ "_id" : ObjectId("51780f796ec4051a536015d0"), "name" : "Sam" }
{ "_id" : ObjectId("51780f796ec4051a536015d1"), "name" : "Chris" }
{ "_id" : ObjectId("51780f796ec4051a536015d2"), "name" : "Joe" }
The second collection contains courses:
{
"_id" : ObjectId("51780fb5c9c41825e3e21fc4"),
"name" : "CS 101",
"students" : [
ObjectId("51780f796ec4051a536015cf"),
ObjectId("51780f796ec4051a536015d0"),
ObjectId("51780f796ec4051a536015d2")
]
}
{
"_id" : ObjectId("51780fb5c9c41825e3e21fc5"),
"name" : "Literature",
"students" : [
ObjectId("51780f796ec4051a536015d0"),
ObjectId("51780f796ec4051a536015d0"),
ObjectId("51780f796ec4051a536015d2")
]
}
{
"_id" : ObjectId("51780fb5c9c41825e3e21fc6"),
"name" : "Physics",
"students" : [
ObjectId("51780f796ec4051a536015cf"),
ObjectId("51780f796ec4051a536015d0")
]
}
Each course document contains students array which has a list of students registered for the course. When a student views a course on a web page he needs to see if he has already registered for the course or not. In order to do that, when the courses collection gets queried on the student's behalf, we need to find out if students array already contains the student's ObjectId. Is there a way to specify in the projection of a find query to retrieve student ObjectId from students array only if it is there?
I tried to see if I could $elemMatch operator but it is geared towards an array of sub-documents. I understand that I could use aggregation framework but it seems that it would be on overkill in this case. Aggregation framework would probably not be as fast as a single find query. Is there a way to query course collection to so that the returned document could be in a form similar to this?
{
"_id" : ObjectId("51780fb5c9c41825e3e21fc4"),
"name" : "CS 101",
"students" : [
ObjectId("51780f796ec4051a536015d0"),
]
}
[edit based on this now being possible in recent versions]
[Updated Answer] You can query the following way to get back the name of class and the student id only if they are already enrolled.
db.student.find({},
{_id:0, name:1, students:{$elemMatch:{$eq:ObjectId("51780f796ec4051a536015cf")}}})
and you will get back what you expected:
{ "name" : "CS 101", "students" : [ ObjectId("51780f796ec4051a536015cf") ] }
{ "name" : "Literature" }
{ "name" : "Physics", "students" : [ ObjectId("51780f796ec4051a536015cf") ] }
[Original Answer] It's not possible to do what you want to do currently. This is unfortunate because you would be able to do this if the student was stored in the array as an object. In fact, I'm a little surprised you are using just ObjectId() as that will always require you to look up the students if you want to display a list of students enrolled in a particular course (look up list of Id's first then look up names in the students collection - two queries instead of one!)
If you were storing (as an example) an Id and name in the course array like this:
{
"_id" : ObjectId("51780fb5c9c41825e3e21fc6"),
"name" : "Physics",
"students" : [
{id: ObjectId("51780f796ec4051a536015cf"), name: "John"},
{id: ObjectId("51780f796ec4051a536015d0"), name: "Sam"}
]
}
Your query then would simply be:
db.course.find( { },
{ students :
{ $elemMatch :
{ id : ObjectId("51780f796ec4051a536015d0"),
name : "Sam"
}
}
}
);
If that student was only enrolled in CS 101 you'd get back:
{ "name" : "Literature" }
{ "name" : "Physics" }
{
"name" : "CS 101",
"students" : [
{
"id" : ObjectId("51780f796ec4051a536015cf"),
"name" : "John"
}
]
}
It seems like the $in operator would serve your purposes just fine.
You could do something like this (pseudo-query):
if (db.courses.find({"students" : {"$in" : [studentId]}, "course" : courseId }).count() > 0) {
// student is enrolled in class
}
Alternatively, you could remove the "course" : courseId clause and get back a set of all classes the student is enrolled in.
I am trying to explain by putting problem statement and solution to it. I hope it will help
Problem Statement:
Find all the published products, whose name like ABC Product or PQR Product, and price should be less than 15/-
Solution:
Below are the conditions that need to be taken care of
Product price should be less than 15
Product name should be either ABC Product or PQR Product
Product should be in published state.
Below is the statement that applies above criterion to create query and fetch data.
$elements = $collection->find(
Array(
[price] => Array( [$lt] => 15 ),
[$or] => Array(
[0]=>Array(
[product_name]=>Array(
[$in]=>Array(
[0] => ABC Product,
[1]=> PQR Product
)
)
)
),
[state]=>Published
)
);

Mongoose find returns document instead of specific object in array

When I try to find specific object in array using find({query}) I always get all elements from array.
Activities array stores activities (it would be a thousands of them) as you can see in the following snippet:
This is my collection:
{
"_id" : ObjectId("58407140755324d04db2ce95"),
"owner" : 103429326776572,
"activities" : [
{
"name" : "test1",
"startTime" : ISODate("2016-08-11T17:41:54Z"),
"type" : "te1",
"lat" : 1,
"lon" : 1,
"creator" : 126212904493088,
"coverPhoto" : {
"name" : "test1",
"path" : "c:\\Users\\Francis\\Desktop\\dusk\\public\\coverPhotos\\SJ9tpP6Mx.jpg"
},
"identifier" : "H1g9F6vpGl",
"users" : [
1,
2,
3
],
"hashTags" : [
"some",
"hashtags"
]
},
{
"name" : "test2",
"startTime" : ISODate("2016-08-11T17:41:53Z"),
"type" : "te2",
"lat" : 1,
"lon" : 1,
"creator" : 103312904493090,
"coverPhoto" : {
"name" : "test2",
"path" : "c:\\Users\\Francis\\Desktop\\dusk\\public\\coverPhotos\\Hy8qpvafe.jpg"
},
"identifier" : "rJlU5TvpMx",
"users" : [
1,
2,
3
],
"hashTags" : [
"some",
"hashtags"
]
}
]
}
I need to get for example an activity that has specific identifier.
I tried to use queries like:
1) db.myCollection.find({'activities.identifier' : "rJlU5TvpMx"})
2) db.myCollection.find({'activities' : { $elemMatch : { "identifier" : "rJlU5TvpMx", "creator" : 103312904493090 } })
And all combinations with '' or "" signs
I found above queries at mongodb docs in equal documents schema as mine is.
Can you tell me what am I doing wrong ?
You can try either use single match or multiple match based on your need. This makes use of $elemMatch(projection)
db.myCollection.find({"_id" : ObjectId("58407140755324d04db2ce95")},
{activities: {$elemMatch: { identifier: "rJlU5TvpMx"}}})
db.myCollection.find( {"_id" : ObjectId("58407140755324d04db2ce95")},
{activities: {$elemMatch: {creator : 103312904493090, identifier: "rJlU5TvpMx" }}})
You are looking for the projection object which gets passed as an argument in your query. It allows the return of specific fields from your search rather than the entire document. http://mongoosejs.com/docs/api.html#model_Model.find
I would also suggest looking at the response to this question here: Mongoose Query: Find an element inside an array which makes use of the unwind operator to enter the array as it seems to be relevant to your needs.
In the collection you are searching in, you have just one Document(Object). If you apply method find() to your collection and the query inside matches the value in activities.identifier it will return the only Document(object).
To have a better understanding of what I am talking about check example on mongoose API doc
And query result here.
Try check this out https://docs.mongodb.com/v3.0/reference/operator/projection/elemMatch/#proj._S_elemMatch instead

Push currentUser's ID to inner Array of a collection/ document in meteorjs

{
"_id" : "s2QBCnv6fXv5YbjAP",
"question" : "Is this real change?",
"createdAt" : ISODate("2016-05-13T21:05:23.381Z"),
"yes" : [
{
"heading" : "Yes It is",
"body" : "I think this government knows what they are doing. That's why there has not been any form of protest",
"email" : "I think this government knows what they are doing. That's why there has not been any form of protest",
"createdAt" : ISODate("2016-05-13T21:08:25.119Z"),
"replies" : [ ],
"likes" : [ ]
},
{
"heading" : "Well, Yes",
"body" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time",
"email" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time",
"createdAt" : ISODate("2016-05-13T21:10:47.123Z"),
"replies" : [ ],
"likes" : [ ]
}
],
"no" : [
{
"heading" : "Not at All",
"body" : "This is not the change I wanted. This is waste of four years and I amm waiting to see the promised change",
"email" : "kenshin#kay.com",
"createdAt" : ISODate("2016-05-13T21:12:58.977Z"),
"replies" : [ ],
"likes" : [ ]
}
],
"author" : "admin",
"image" : "/cfs/files/QuestionImages/DzdpK6NdurZMTwAse"
}
Hi all, I'm quite new to MeteorJs and I'm working on an app. i would like to know how to update the "likes" array. I want to push the currentUser's Id into the likes so on the front-end I will display yes.objectsReference.likes.length as the number of likes.
How can i target the likes array? thanks
You can use the $ positional operator on the server to update the likes array. Suppose you want to add the current user id to the likes array on the embedded "Yes" document with "heading" value "Well, Yes", then the following shows how you can do it on the server:
Articles.update(
{
"_id" : "s2QBCnv6fXv5YbjAP",
"yes.heading" : "Well, Yes"
},
{
"$push": { "yes.$.likes": userId }
}
)

Query to Match on nth Document of an Array

I am new to MongoDB and I am doing some exercises on it. In particular I got stuck on this exercise, of which I report here the question:
Given the following structure for document "Restaurant":
{
"_id" : ObjectId("5704adbc2eb7ebe23f582818"),
"address" : {
"building" : "1007",
"coord" : [
-73.856077,
40.848447
],
"street" : "Morris Park Ave",
"zipcode" : "10462"
},
"borough" : "Bronx",
"cuisine" : "Bakery",
"grades" : [
{
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
},
{
"date" : ISODate("2013-09-11T00:00:00Z"),
"grade" : "A",
"score" : 6
},
{
"date" : ISODate("2013-01-24T00:00:00Z"),
"grade" : "A",
"score" : 10
},
{
"date" : ISODate("2011-11-23T00:00:00Z"),
"grade" : "A",
"score" : 9
},
{
"date" : ISODate("2011-03-10T00:00:00Z"),
"grade" : "B",
"score" : 14
}
],
"name" : "Morris Park Bake Shop",
"restaurant_id" : "30075445"
}
Write a MongoDB query to find the restaurant Id, name and grades for those restaurants where 2nd element of grades array contains a grade of "A" and score 9 on an ISODate "2014-08-11T00:00:00Z".
I wrote this query:
db.restaurants.find(
{
'grades.1': {
'score': 'A',
'grade': 9,
'date' : ISODate("2014-08-11T00:00:00Z")
}
},
{
restaurant_id: 1,
name: 1,
grades: 1
});
which is not working.
The solution provided is the following:
db.restaurants.find(
{ "grades.1.date": ISODate("2014-08-11T00:00:00Z"),
"grades.1.grade":"A" ,
"grades.1.score" : 9
},
{"restaurant_id" : 1,"name":1,"grades":1}
);
My questions are:
is there a way to write the query avoiding to repeat the grades.1 part?
Why is my query wrong, given that grades.1 is a document object?
If it can help answering my question, I am using MongoDB shell version: 3.2.4
EDIT:
I found an answer to question 2 thanks to this question.
In particular I discovered that order matters. Indeed, if I perform the following query, I get a valid result:
db.restaurants.find({'grades.1': {'date': ISODate("2014-08-11T00:00:00Z"), 'grade':'A', score:9}}, {restaurant_id:1, name:1, grades:1})
Note that this query works only because all subdocument's "fields" are specified, and they are specified in the same order.
Not really. But perhaps an explanation of what you "can" do:
db.junk.find({
"grades": {
"$elemMatch": {
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
}
},
"$where": function() {
var grade = this.grades[0];
return (
grade.date.valueOf() == ISODate("2014-03-03T00:00:00Z").valueOf() &&
grade.grade === "A" &&
grade.score ==== 2
)
}
})
The $elemMatch allows you to shorten a little, but it is not the "nth" element of the array. In order to narrow that further you need to use the $where clause to inspect the "nth" array element to see if all values are a match.
db.junk.aggregate([
{ "$match": {
"grades": {
"$elemMatch": {
"date" : ISODate("2014-03-03T00:00:00Z"),
"grade" : "A",
"score" : 2
}
}
}},
{ "$redact": {
"$cond": {
"if": {
"$let": {
"vars": { "grade": { "$arrayElemAt": [ "$grades", 0 ] } },
"in": {
"$and": [
{ "$eq": [ "$grade.date", ISODate("2014-03-03T00:00:00Z") ] },
{ "$eq": [ "$grade.grade", "A" ] },
{ "$eq": [ "$grade.score", 2 ] }
]
}
}
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}}
])
You can do the same logic with $redact as well using .aggregate(). It runs a little quicker, but the basic truth should be clear by now.
So using "dot notation" to specify the "nth" position for each element within the array like you have already done is the most efficient and "brief" way to write this. You cannot make it shorter or better.
Your other attempt is looking for a "document" within "grades.1" that matches exactly the document condition you are providing. If for any reason those are not the only fields present, or if they are indeed in "different order" in the stored document, then such a query condition will not be a match.

Categories

Resources