Get number of followers for specific page in Mongodb - database

let's say I have a collection called pages as
{
_id: "pageid",
name: "Mongodb"
},
{
_id: "pageid2",
name: "Nodejs"
}
and user collection as follows
{
_id : "userid1",
following: ["pageid"],
...
},
{
_id : "userid2",
following: ["pageid", "pageid2"],
...
}
how could I make a query to retrieve the pages information along with the number of users follow each page in mongodb, expected result as follows
[
{
_id: "pageid",
name: "MongoDB",
followers: 2
},
{
_id: "pageid2",
name: "Nodejs",
followers: 1
},
]

You can use $lookup and $size to count total followers,
db.pages.aggregate([
{
$lookup: {
from: "user",
localField: "_id",
foreignField: "following",
as: "followers"
}
},
{
$addFields: {
followers: { $size: "$followers" }
}
}
])
Playground

Related

MongoDB Aggregation - Fetch data from ObjectId subarray

I have these collections:
author
[
{
_id: "63c242130b17d5516e0cb499",
author_name:'Vyom',
book_ids:["63c242330b17d5516e0cb49a","63c242410b17d5516e0cb49b"]
}
]
book
[
{
_id:"63c242330b17d5516e0cb49a",
author_id:'63c242130b17d5516e0cb499',
book_name:'True Love',
genere:'horror'
},
{
_id:"63c242410b17d5516e0cb49b",
author_id:'63c242130b17d5516e0cb499',
book_name:'Monster Strike',
genere:'romance'
},
]
I want to fetch details of books in author collection aggregation if book_ids exists.
For this I tried as:
db.author.aggregate([
{
$match: {
_id: ObjectId("63c242130b17d5516e0cb499")
}
},
{
$lookup:{
from: 'book',
localField: '_id',
foreignField: 'author_id',
as: 'book_details'
}
},
{
$addFields:{
book_info: {
$map: {
input: '$book_details'
as: 'el'
in: {
$match: {_id:ObjectId('$$el._id')},
$paroject: {book_name: 1},
}
}
}
}
}
])
But it throws:
Unrecognized error: '$match'.
Expected O/P:
[
{
_id: "63c242130b17d5516e0cb499",
author_name:'Vyom',
book_ids:["63c242330b17d5516e0cb49a","63c242410b17d5516e0cb49b"],
book_info: [
{
_id:"63c242330b17d5516e0cb49a",
book_name:'True Love',
},
{
_id:"63c242410b17d5516e0cb49b",
book_name:'Monster Strike',
}
]
}
]
Is there any other way to loop and get details? I tried looking for other solutions but was unable to find.
I don't see why you need a $set stage to format the element in the book_details array for the book_info field.
You can use $lookup with pipeline to join both collections and format the array of documents returned.
db.author.aggregate([
{
$match: {
_id: ObjectId("63c242130b17d5516e0cb499")
}
},
{
$lookup: {
from: "book",
localField: "book_ids",
foreignField: "_id",
as: "book_info",
pipeline: [
{
$project: {
_id: 1,
book_name: 1
}
}
]
}
}
])
Demo # Mongo Playground
db.authors.aggregate([
{
$lookup: {
from: "books",
localField: "book_ids",
foreignField: "_id",
as: "books"
}
},
{
$match: { "books": { $ne: [] } }
},
{
$project: {
_id: 1,
author_name: 1,
books: {
_id: 1,
author_id: 1,
book_name: 1,
genre: 1
}
}
}
])
This pipeline will give you the result where all the authors' book details will be there if they have any books

Lookup VS Lookup with pipeline MongoDB (Performace & How it internally works)

I'm making a blog and have an query about which would give me better performace, simple lookup or lookup with pipeline because sometime simple lookup gave me fast result and sometime pipleline lookup. So, I am bit confused now which one to use or where to use. Suppose I have 2 collection, user and comment collection.
// Users Collection
{
_id: "MONGO_OBJECT_ID",
userName: "Web Alchemist"
}
// Comments Collection
{
_id: "MONGO_OBJECT_ID",
userId: "USER_MONGO_OBJECT_ID",
isActive: "YES", // YES or NO
comment: "xyz"
}
Now I want to Lookup from users collection to comments, which one would be better for this. I made two query which giving me same result.
[
{
$match: { _id: ObjectId("5d68c019c7d56410cc33b01a") }
},
{
$lookup: {
from: "comments",
as: "comments",
localField: "_id",
foreignField: "userId"
}
},
{
$unwind: "$comments"
},
{
$match: {
"comments.isActive": "YES"
}
},
{ $limit: 5},
{
_id: 1, userName: 1, comments: { _id: "$comments._id", comment: "$comments.comment"}
},
{
$group: {
_id: "$_id",
userName: { '$first': '$userName' },
comments: { $addToSet: "comments"}
}
}
]
OR
[
{
$match: { _id: ObjectId("5d68c019c7d56410cc33b01a") }
},
{
$lookup: {
from: "comments",
as: "comments",
let: { userId: "$_id" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ['$userId', '$$userId'] },
{ $eq: ['$isActive', 'YES'] }
]
}
}
},
{ limit: 5 },
{
$project: { _id: 1, comment: 1 }
}
]
}
}
]

MongoDB Aggregate ObjectId inside Array of embedded Documents

I have seen a ton of question about the $lookup aggregator for arrays of ObjectIds but I can't seem to find anything about when the ObjectId is inside an array of embedded documents.
I have the follow document inside a mongodb database:
_id: ObjectId('...')
Alarms: [
{
Gateway: ObjectId('...')
Port: 1
},
{
Gateway: ObjectId('...')
Port: 2
}
]
I would like to have the following:
_id: ObjectId('...')
Alarms [
{
Gateway: ...(Gateway Object),
Port: 1
},
{
Gateway: ...(Gateway Object),
Port: 2
}
]
I have tried the following with no success:
$lookup: {
from: 'Gateway',
localField: 'Alarms.Gateway',
foreignField: '_id',
as: 'Alarms.Gateway'
}
But this gives me the following result:
_id: ObjectId('...')
Alarms [
{
Gateway: {
...(Gateway Object)
}
Port: 1
}
]
Please try the below queries :
If you don't want the object which doesn't have match in Gateway collection exist in Alarms array in final result :
db.Alarms.aggregate([{ $unwind: '$Alarms' }, {
$lookup: {
from: 'Gateway',
localField: 'Alarms.Gateway',
foreignField: '_id',
as: 'Alarms.Gateway'
}
}, { $match: { 'Alarms.Gateway': { $ne: [] } } },
{ $addFields: { 'Alarms.Gateway': { $arrayElemAt: ['$Alarms.Gateway', 0] } } },
{ $group: { _id: '$_id', Alarms: { $push: '$Alarms' } } }
])
Test : MongoDB-Playground
Otherwise, if you want all objects in Alarms array to be returned irrespective of whether there is a match in Gateway or not :
db.Alarms.aggregate([{ $unwind: '$Alarms' }, {
$lookup: {
from: 'Gateway',
localField: 'Alarms.Gateway',
foreignField: '_id',
as: 'Alarms.GatewayObj'
}
}, { $addFields: { 'Alarms.Gateway': { $cond: [{ $ne: ['$Alarms.GatewayObj', []] }, { $arrayElemAt: ['$Alarms.GatewayObj', 0] }, '$Alarms.Gateway'] } } },
{ $project: { 'Alarms.GatewayObj': 0 } },
{ $group: { _id: '$_id', Alarms: { $push: '$Alarms' } } }
])
Test : MongoDB-Playground
Difference between two queries would be one will return below object in Alarms array (Vs) one don't.
{
"Gateway": ObjectId("5e2b5425d02e05b6940de2fb"),
"Port": 2
}

get sum conditional with mongodb

I need to get the sells sum, just with the idCategory, any idea?
I have this 3 schemas in mongondb
category = [{"id":1,"name":"cat1"}, {"id":2,"name":"cat2"}]
product = [{"id":1,"name":"product1", "catId":1}, {"id":2,"name":"product2", "catId":2}]
sells = [{"id":1,"value":80, "productId":1, status:'active'}, {"id":2,"value":90, "productId":2, status:'Inactive'}]
MongoDB collections are not actual schemas, also, I'm assuming:
You're interpreting them as arrays (although they aren't), so you mean each of your Collections have 2 documents inside of them.
You want the sum of sales grouped by product category.
If those are the cases, what you want is a MongoDB Aggregate which you would run on your "sells" collection, "join" with the "product" collection and group by category id.
The base aggregate to do so would be along the following lines:
sells.aggregate([
{
$lookup: {
from: "product",
localField: "productId",
foreignField: "id",
as: "ProductData"
}
},
{
$unwind: "$ProductData"
},
{
$group: {
_id: "$ProductData.catId",
total: { $sum: "$value" }
}
}
]);
If you also want to fetch the Category name after aggregating, all you need to do is insert another $lookup at the end of the pipeline joining with category collection:
sells.aggregate([
{
$lookup: {
from: "product",
localField: "productId",
foreignField: "id",
as: "ProductData"
}
},
{
$unwind: "$ProductData"
},
{
$group: {
_id: "$ProductData.catId",
total: { $sum: "$value" }
}
},
{
$lookup: {
from: "category",
localField: "_id",
foreignField: "id",
as: "CategoryData"
}
},
{
$unwind: "$CategoryData"
},
{
$project: {
name: "$CategoryData.name",
total: 1
}
}
]);
EDIT (adding new case request on comment):
db.getCollection('product').aggregate([
{
$lookup: {
from: "sells",
localField: "id",
foreignField: "productId",
as: "SalesData"
}
},
{
$unwind:
{
path: "$SalesData",
preserveNullAndEmptyArrays: true
}
},
{
$project: {
catId: 1,
value: "$SalesData.value"
}
},
{
$group: {
_id: "$catId",
total: { $sum: "$value" }
}
},
{
$lookup: {
from: "category",
localField: "_id",
foreignField: "id",
as: "CategoryData"
}
},
{
$unwind: "$CategoryData"
},
{
$project: {
name: "$CategoryData.name",
total: 1
}
}
]);

MongoDB - How to verify if a nested array item is contained in another nested array item on the same document?

The idea here is to return an array of documents of the users' followers with the information if this user is a friend of that follower or not.
So far I have:
db.getCollection('users').aggregate([
{ $match: { _id: ObjectId("588877d82523b4395039910a") } },
{ $lookup: {
from: 'users',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
},
{
$project: {
"s_followers._id": 1,
"s_followers.isFriend": {
$in: ["s_followers.id",
{ $setIntersection: ["$friends", "$followers"] }
]}
}
}
])
But the "s_followers.id" used in the $in operator doesn't seem to retrieve the _id information from the follower, so it always returns false.
When I use a ObjectId directly, I got the result I want:
"s_followers.isFriend": {
$in: [ObjectId("588877d82523b4395039910a"),
{ $setIntersection: ["$friends", "$followers"] }
]}
But I really need this ID to be a reference to the follower _id.
Expected result would be something like:
{
"_id" : ObjectId("588877d82523b4395039910a"),
"s_followers" : [
{
"_id" : ObjectId("5888687e56be8f172844d96f"),
"isFriend" : true
},
{
"_id" : ObjectId("5888ca27d79b8b03949a6e8c"),
"isFriend" : false
}
]
}
Thanks for your help!
UPD: A different approach (maybe easier), would be to use the ID of the user that I have (the one used on $match), but I would still need to get the reference for the follower's follower array
db.getCollection('users').aggregate([
{ $match: { _id: ObjectId("588877d82523b4395039910a") } },
{ $lookup: {
from: 'users',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
}, {
$project: {
"firstName": 1,
"s_followers._id": 1,
"s_followers.firstName": 1,
"s_followers.followers": 1,
"s_followers.isFriend": { $in: [ObjectId("588877d82523b4395039910a"), "$s_followers.followers"] }
}
}
])
UPD2: The user data structure (the part that matters)
{
followers: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
friends: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
}
FOR VERSION 3.4.0+
Ok, just got it, I'll post here the code and my understanding of it:
db.getCollection('users').aggregate([
{ $match: { _id: ObjectId("588877d82523b4395039910a") } },
{ $lookup: {
from: 'users',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
}, {
$project: {
"firstName": 1,
"s_followers._id": 1,
"s_followers.firstName": 1,
"s_followers.followers": 1,
}
}, {
$unwind: "$s_followers"
}, {
$project: {
"firstName": "$s_followers.firstName",
"isFriend": { $in: [ObjectId("588877d82523b4395039910a"), "$s_followers.followers"] }
}
}
])
My understanding of it:
$match: match the user I'm intended to get the followers of.
$lookup: found each follower detail
$project: select the information I want to return, also get the follower list of each follower
$unwind: create a different document for each follower
With the array unwinded, I can refer to the follower's followers array, and find my object id in it :)
In my example use followers friends list to check current user is friend or not. as {$arrayElemAt:["$s_followers.friends",0]} if want to find in followers then can use "$s_followers.followers"
You can try it.
db.getCollection('user').aggregate([
{ $match: { _id: ObjectId("5714d190e6128b7e7f8d9008") } },
{$unwind:"$followers"},
{ $lookup: {
from: 'user',
localField: 'followers',
foreignField: '_id',
as: 's_followers'
}
},
{$project:{
firstName:1,
s_followers:{$arrayElemAt:["$s_followers",0]},
isFriend:{$cond:[{
$anyElementTrue:{
$map: {"input": {$arrayElemAt:["$s_followers.friends",0]},
"as": "el",
"in": { "$eq": [ "$$el", "$_id" ] }
}
}
},true,false]}
}
},
{$group:{
_id:"$_id",
s_followers:{$push:{_id:"$s_followers._id",isFriend:"$isFriend"}}
}
}
])

Resources