I've just got stuck with this problem. I've got two Mongoose schemas:
var childrenSchema = mongoose.Schema({
name: {
type: String
},
age: {
type: Number,
min: 0
}
});
var parentSchema = mongoose.Schema({
name : {
type: String
},
children: [childrenSchema]
});
Question is, how to fetch all subdocuments (in this case, childrenSchema objects) from every parent document? Let's suppose I have some data:
var parents = [
{ name: "John Smith",
children: [
{ name: "Peter", age: 2 }, { name: "Margaret", age: 20 }
]},
{ name: "Another Smith",
children: [
{ name: "Martha", age: 10 }, { name: "John", age: 22 }
]}
];
I would like to retrieve - in a single query - all children older than 18. Is it possible? Every answer will be appreciated, thanks!
You can use $elemMatch as a query-projection operator in the most recent MongoDB versions. From the mongo shell:
db.parents.find(
{'children.age': {$gte: 18}},
{children:{$elemMatch:{age: {$gte: 18}}}})
This filters younger children's documents out of the children array:
{ "_id" : ..., "children" : [ { "name" : "Margaret", "age" : 20 } ] }
{ "_id" : ..., "children" : [ { "name" : "John", "age" : 22 } ] }
As you can see, children are still grouped inside their parent documents. MongoDB queries return documents from collections. You can use the aggregation framework's $unwind method to split them into separate documents:
> db.parents.aggregate({
$match: {'children.age': {$gte: 18}}
}, {
$unwind: '$children'
}, {
$match: {'children.age': {$gte: 18}}
}, {
$project: {
name: '$children.name',
age:'$children.age'
}
})
{
"result" : [
{
"_id" : ObjectId("51a7bf04dacca8ba98434eb5"),
"name" : "Margaret",
"age" : 20
},
{
"_id" : ObjectId("51a7bf04dacca8ba98434eb6"),
"name" : "John",
"age" : 22
}
],
"ok" : 1
}
I repeat the $match clause for performance: the first time through it eliminates parents with no children at least 18 years old, so the $unwind only considers useful documents. The second $match removes $unwind output that doesn't match, and the $project hoists children's info from subdocuments to the top level.
In Mongoose, you can also use the elegant .populate() function like this:
parents
.find({})
.populate({
path: 'children',
match: { age: { $gte: 18 }},
select: 'name age -_id'
})
.exec()
A. Jesse Jiryu Davis's response works like a charm, however for later versions of Mongoose (Mongoose 5.x) we get the error:
Mongoose 5.x disallows passing a spread of operators to Model.aggregate(). Instead of Model.aggregate({ $match }, { $skip }), do Model.aggregate([{ $match }, { $skip }])
So the code would simply now be:
> db.parents.aggregate([{
$match: {'children.age': {$gte: 18}}
}, {
$unwind: '$children'
}, {
$match: {'children.age': {$gte: 18}}
}, {
$project: {
name: '$children.name',
age:'$children.age'
}
}])
{
"result" : [
{
"_id" : ObjectId("51a7bf04dacca8ba98434eb5"),
"name" : "Margaret",
"age" : 20
},
{
"_id" : ObjectId("51a7bf04dacca8ba98434eb6"),
"name" : "John",
"age" : 22
}
],
"ok" : 1
}
(note the array brackets around the queries)
Hope this helps someone!
Related
this is my first time asking in StackOverflow and I hope I can explain what I'm aiming for.
I've got documents that look like this:
"_id" : ObjectId("5fd76b67a7e0fa652a297a9f"),
"type" : "play",
"session" : "5b0b5d57-c3ca-415f-8ef6-49bbd5805a23",
"episode" : 1,
"show" : 1,
"user" : 1,
"platform" : "spotify",
"currentTime" : 0,
"date" : ISODate("2020-12-14T13:40:51.906Z"),
"__v" : 0
}
I'd like to fetch for a show and group them by episode. I've got this far with my aggregattion:
const filter = { user, show, type: { $regex: /^(play|stop|close)$/ } }
const requiredFields = { "episode": 1, "session": 1, "date": 1, "currentTime": 1 }
// Get sessions grouped by episode
const it0 = {
_id: '$episode',
session:
{$addToSet:
{_id: "$session",
date:{$dateToString: { format: "%Y-%m-%d", date: "$date" }},
averageOfSession: {$cond: [ { $gte: [ "$currentTime", 0.1 ] }, "$currentTime", null ] }
},
},
count: { $sum: 1 }
}
// Filter unique sessions by session id and add them to a sessions field
const reduceSessions = {$addFields:
{sessions: {$reduce: {input: "$session",initialValue: [],in:
{$concatArrays: ["$$value",{$cond: [{$in: ["$$this._id","$$value._id"]},[],["$$this"]]}]}
}}}}
const projection = { $project: { _id: 0, episode: "$_id", plays: {$size: '$sessions'}, dropoff: {$avg: "$sessions.averageOfSession"}, sessions: '$session.date', events: "$count" } }
const arr = await Play.aggregate([
{ $match: filter }, {$project: requiredFields}, {$group: it0}, reduceSessions,
projection,{ $sort : { _id : 1 } }
])
and this is what my result looks like so far:
{
"episode": 5,
"plays": 4,
"dropoff": 3737.25,
"sessions": [
"2020-11-15",
"2020-11-15",
"2020-11-16",
"2020-11-15"
],
"events": 4
}...
What I'd like is for the 'sessions' array to be an object with one key for each distinct date which would contain the count, so something like this:
{
"episode": 5,
"plays": 4,
"dropoff": 3737.25,
"sessions": {
"2020-11-15": 3,
"2020-11-16": 1
},
"events": 4
}...
Hope that makes sense, thank you!!
You can first map sessions into key-value pairs. Then $group them to add up the sum. Then use $arrayToObject to convert to the format you want.
This Mongo playground is referencing this example.
Here is the collection.
{{
"id" : "123",
"likes" : [ {
"member" : "3041"
},
{
"member" : "3141"
}]
},
{
"id" : "124",
"likes" : [ {
"member" : "3241"
},
{
"member" : "3241"
},
{
"member" : "3341"
}]
}}
How to retrieve the count of number of objects of likes key for each document?
In this format:
[{
"id" : "123",
"likesCount" : 2
},
{
"id" : "124",
"likesCount" : 3
}]
This should do it for you:
db.collection.aggregate([
{
$project: {
_id: 0,
id: 1,
likesCount: {
$size: "$likes"
}
}
}
])
You are using an aggregation and in the $project part of it use $size to get the length of the array.
You can see it working here
I think you have to map the collection to transform the objects within it into the shape you want.
In this case we get the object and extract the id and instead of all the likes we just get the length of them.
let newCollection = collection.map(obj => ({
id: obj.id,
likesCount: obj.likes.length
}));
For a collection with 200k documents like the following:
{
name: Mario,
profession: plumber,
level: 8,
},
{
name: Luigi,
profession: plumber,
level: 5,
},
{
name: Walter,
profession: cook,
level: 10,
},
{
name: Jesse,
profession: cook,
level: 3,
}
What would be an efficient query to get only a single document per profession, with or without sorting by level?
To expand on #felix answer, you can do this easily with a MongoDB group operation. For example, in the MongoDB shell:
db.yourCollection.aggregate([
{ $group: { _id: '$profession', doc: { $first: '$$ROOT' } } }
])
will give you results like:
{
_id: 'plumber',
doc: {
name: 'Mario',
profession: 'plumber',
level: 8
}
},
{
_id: 'cook',
doc: {
...
}
},
...
If more fine-grained control is required, you can filter the results before or after the grouping with $match. Any MongoDB adaptor for Node should give you access to this method.
use the $group and aggregate() function to get your expected result
Query:
db.getCollection('agg').aggregate([
{ $group: { _id: '$profession', doc: { $first: '$$ROOT' }, count: { $sum: 1 } } }
])
Output:
/* 1 */
{
"_id" : "cook",
"doc" : {
"_id" : ObjectId("59157ace263af55a862b78dc"),
"name" : "Walter",
"profession" : "cook",
"level" : 10.0
},
"count" : 2.0
}
/* 2 */
{
"_id" : "plumber",
"doc" : {
"_id" : ObjectId("59157ace263af55a862b78da"),
"name" : "Mario",
"profession" : "plumber",
"level" : 8.0
},
"count" : 2.0
}
I have a collection db.activities, each item of which has a dueDate. I need to present data in a following format, which basically a list of activities which are due today and this week:
{
"today": [
{ _id: 1, name: "activity #1" ... },
{ _id: 2, name: "activity #2" ... }
],
"thisWeek": [
{ _id: 3, name: "activity #3" ... }
]
}
I managed to accomplish this by simply querying for the last week's activities as a flat list and then grouping them with javascript on the client, but I suspect this is a very dirty solution and would like to do this on server.
look up mongo aggregation pipeline.
your aggregation has a match by date, group by date and a maybe a sort/order stage also by date.
lacking the data scheme it will be along the lines of
db.collection.aggregate([{ $match: {"duedate": { "$gte" : start_dt, "$lte" : end_dt} } ,
{ $group: {_id: "$duedate", recordid : "$_id" , name: "$name" },
{"$sort" : {"_id" : 1} } ] );
if you want 'all' records remove the $match or use { $match: {} } as one does with find.
in my opinion, you cannot aggregate both by day and week within one command. the weekly one may be achieved by projecting duedate using mongos $dayOfWeek. along the lines of
db.collection.aggregate([
{ $match: {"duedate": { "$gte" : start_dt, "$lte" : end_dt} } ,
{ $project : { dayOfWeek: { $dayOfWeek: "$duedate" } },
{ $group: {_id: "$dayOfWeek", recordid : "$_id" , name: "$name" },
{"$sort" : {"_id" : 1} } ] );
check out http://docs.mongodb.org/manual/reference/operator/aggregation/dayOfWeek/
I have a MongoDB collection [Users] each with an Array of embedded documents [Photos].
I would like to be able to have a page which lists recent photos, for example - the last 10 photos uploaded by any user.
Users
[
{
_id: ObjectID
name: "Matthew"
Photos: [
{
_id: ObjectID
url: "http://www.example.com/photo1.jpg"
created: Date("2013-02-01")
},
{
_id: ObjectID
url: "http://www.example.com/photo3.jpg"
created: Date("2013-02-03")
}
]
},
{
_id: ObjectID
name: "Bob"
Photos: [
{
_id: ObjectID
url: "http://www.example.com/photo2.jpg"
created: Date("2013-02-02")
},
{
_id: ObjectID
url: "http://www.example.com/photo4.jpg"
created: Date("2013-02-04")
}
]
}
]
My first attempt at solving this was to find all Users where Photos is not null, and sort it by the created date:
User.find({images:{$ne:[]}}).sort({"images.created":-1})
Unfortunately, this doesn't work as I need it to. It sorts Users by the most recent images, but still returns all images, and there is no way to limit the number of images returned by the function.
I started looking into aggregate, and it seems like it might be the solution I'm looking for, but I'm having trouble finding out how to get it to work.
Ideally, the type of result I would like returned would be like this:
results: [
{
_id: ObjectID (from Photo)
name: "Bob"
url: "http://www.example.com/photo4.jpg"
created: Date("2013-02-04")
}
{
_id: ObjectID (from Photo)
name: "Matthew"
url: "http://www.example.com/photo3.jpg"
created: Date("2013-02-03")
}
]
Each result should be a Photo, and I should be able to limit the results to a specific amount, and skip results for paged viewing.
Please let me know if you need any more information, and thank you for your responses.
You need aggregation framework:
db.users.aggregate(
{ $project: {"Photos" : 1, _id: 0, name:1 }},
{ $unwind: "$Photos" },
{ $sort: {"Photos.created" : -1} },
{ $skip: 1 },
{ $limit: 2 }
)
And result would be like this:
{
"result" : [
{
"name" : "Matthew",
"Photos" : {
"_id" : 2,
"url" : "http://www.example.com/photo3.jpg",
"created" : "2013-02-03"
}
},
{
"name" : "Bob",
"Photos" : {
"_id" : 3,
"url" : "http://www.example.com/photo2.jpg",
"created" : "2013-02-02"
}
}
],
"ok" : 1
}
I think you want something like this:
db.Users.aggregate( [
{$unwind:"$Photos"},
{$sort: {"Photos.created":-1},
{$limit: 10}
] );