How can I get only one object property data from mongodb collection by using nodejs? - mongodb

From mongodb collection I want to get only one property like if I had a array of object structure
[
{
_id: 'property1',
name: 'property1',
value: 1
},
{
_id: 'property2',
name: 'property2',
value: 2
}
]
So here I don't want to get whole collection, instead I only just need name from all the documents in the collection

Related

How to search a Mongo collection for a document that contains an array that contains an element that meets a particular criteria?

I'm new at Mongo and there might be a better way to do what I want. I'm dealing with a particular data structure that my application must process.
Suppose that I have a collection that contains two documents that contain information about universities and their student clubs to include the name of each club and the name of each student in each club along with their age:
{
_id: 1, // object ID
name: "Oxford University",
clubs: [{
name: "Soccer",
members: [{
name: "Alice",
age: 22
},
{
name: "Bob",
age: 23
}
]
}, {
name: "Gymnastics",
members: [{
name: "Charlie",
age: 20
},
{
name: "Dorothy",
age: 19
}
]
}]
}
{
_id: 2, // object ID
name: "Cambridge University",
clubs: [{
name: "Chess",
members: [{
name: "Ellen",
age: 30
},
{
name: "Frank",
age: 35
}
]
}, {
name: "Go",
members: [{
name: "Gilbert",
age: 25
},
{
name: "Hikari",
age: 40
}
]
}]
}
Suppose that I want to write a query on this collection that will find universities that have a club that has at least one member aged 40 or older.
How do I do that?
I sketched this example based off of the idea of taking some JSON documents and inserting them into a new collection. Maybe it would be a better idea to break this apart into multiple collections. I just had the idea to research if Mongo might be a good product to use in this situation given that a big part of my job here is to create something that can receive some JSON data, process it, and make it queryable.
MongoDB queries have a convenient feature to query documents that have a specific value or condition in embedded objects and arrays. In the query you can specify the "path" to the object or array using "dot notation" without having to specify the exact array index, etc.
Using your example, you can find the documents where a member of a club is aged 40 or older like this.
db.collection.find({
"clubs.members.age": {
"$gte": 40
}
})
This returns the second document in your example collection.
Try it on mongoplayground.net.

MongoDB paginate 2 collections together on common field

I've two mongo collections - File and Folder.
Both have some common fields like name, createdAt etc. I've a resources API that returns a response having items from both collections, with a type property added. type can be file or folder
I want to support pagination and sorting in this list, for example sort by createdAt. Is it possible with aggregation, and how?
Moving them to a container collection is not a preferred option, as then I have to maintain the container collection on each create/update/delete on either of the collection.
I'm using mongoose too, if it has got any utility function for this, or a plugin.
In this case, you can use $unionWith. Something like:
Folder.aggregate([
{ $project: { name: 1, createdAt: 1 } },
{
$unionWith: {
coll: "files", pipeline: [ { $project: { name: 1, createdAt: 1 } } ]
}
},
... // your sorting go here
])

Mongoose/MongoDB - Can I find a document by mapping through an array of ids that it contains?

I have a document model that contains an array of user objects. What I want to do is return the document if that array contains an object with a specific user _id. Is this possible?
Something like
{
_id: 1
name: 'Document with Array of User Ids'
arrayOfUsers: [
{
name: 'John Doe',
_id: 2
},
{
name: 'Michael Scott',
_id: 3
}
]
}
And then returning all documents that have an _id of 3 in arrayOfUsers
Thanks!
You can use dot notation like so:
https://mongoplayground.net/p/iu0f68D1vbL
Model.find({
"arrayOfUsers._id": 2
})
https://docs.mongodb.com/manual/tutorial/query-embedded-documents/#query-on-nested-field

Difference between Reference Mapping and Embedded Mapping

What is the difference between Reference Mapping and Embedded Mapping in Doctrine MongoDB ODM?
I just need to implement one to many relationship.
Embedded documents are stored within the document itself. Referenced documents are stored elsewhere.
A simplified example of how Referenced docs are stored in the db:
//collection one
{
_id: "one_1"
many: [
"many_1",
"many_2",
"many_3"
]
}
//collection many
{
_id: "many_1",
name: "one"
},
{
_id: "many_2",
name: "two"
},
{
_id: "many_3",
name: "three"
}
and Embedded documents:
//collection one
{
_id: "one_1"
many: [
{ _id: "many_1", name: "one"},
{ _id: "many_2", name: "two"},
{ _id: "many_3", name: "three"}
]
}
The former is more flexible, the later is way quicker.
The rule of thumb is to go with references if you need to modify sub-documents individually, or if you need many-to-many refs, or if you can reach the 16MB size limit due to huge number of embedded documents in the foreseeable future.

Pushing data into nested arrays in MongoDB

Whats the best way of pushing data into an array that's nested several levels deep in arrays...
Here's an example document ( made up ) that's representative of the data in the document, except the real document has more schools with more classes and more students. Each 'thing' has a unique id :-
{
_id: 4353467865,
school : [
{
_id: 3425353,
name: 'school of rock',
class: [
{
_id: 3242342
name: 'Room1',
students: [
{ _id: 345456562, name: 'Kevin' }
]
},
{
// more classes with more students nested underneath
}
]
},
{
// more schools, classes, students...
}
]
}
Now I know the id of the document, school and class, and now I want to do an update $push into the students array a {_id: 234554363, name: 'Barry'} for that class.
How do I construct an update query for that?
Do the below 2 links solve your problem?
http://docs.mongodb.org/manual/reference/operator/update/positional/#nested-arrays
Update multi nested array in Mongodb
I'll answer my own question since I've been using Mongo for a while now.
first answer...
When designing Mongo stuff, I was super paranoid to try and get things in one doc if I could since that's the whole benefit. Reality is you don't have to be too paranoid. School and Class could be separate collections. It does depend a bit on how you are using them, but for the most I would do separate collections these days.
second answer...
I have needed to do nested arrays, however any level of nesting array can be separated out into non nested arrays with keys which is then trivial to update using mongo
so
school : [
{
_id: 3425353,
name: 'school of rock',
}
],
classes: [
{
_id: 3242342
school_id: 3425353,
name: 'Room1',
}
],
students: [
{ _id: 345456562, class_id: 3242342, name: 'Kevin' }
{ _id: 345456563, class_id: 3242342, name: 'Gilbert' }
]