I am using aggregation to fetch the value from two collection, One is folder and another is inspections. I am getting all the data's but am getting Inspection count is 0
My code :
mongo.folder.aggregate([
{
$lookup:
{
from: 'inspections',
localField: '_id',
foreignField: 'projectID',
as: 'projectdata'
}
},
{ $match : {
$and: [
querydata
]
}
},
{
"$project": {
"folder_name" : "$folder_name",
"location_name": "$location_name",
"architect_name" :"$architect_name",
"cover_img": "$cover_img",
"created_date" : "$created_date",
"parent_id":"$parent_id",
"share" : "$share",
"share_id":'$share_id',
"user_id": "$user_id",
"customer_id" : "$customer_id",
"download_access" : "$download_access",
"inspection_count": {
"$size": {
"$filter": {
"input": "$projectdata.inspection_data",
"as": "el",
"cond": { "$eq": [ "$$el.published", "Y" ] }
}
}
}
}
}
]).exec(function (err, response) {
console.log("Inside Parent Id 1")
console.log(response)
if(response){
response.splice(0, 0, responseparent);
}
else
{
response = responseparent;
}
if (err){
res.json({ 'folder': [], 'filelist': [] }); } else {
res.json({ 'folder': response, 'filelist': [] }); }
});
Here inspection Count should be 3 But am getting Zero count. How Do i find the size of the array based upon their field value .
I have used filter even though its showing 0 in count
Related
I am trying to using aggregate function for fetching data from two documents. I am able to do it but i am finding a solution how can i apply $project in lookup table only
below is my approach
app.get('/getAllDetailById',(req,res)=>{
if(db){
// lookup
db.collection("points").aggregate(
[
{ "$addFields": { "enquiry_by": { "$toObjectId": "$enquiry_by" }}},
{
"$lookup" : {
from: "user",
localField: "enquiry_by",
foreignField: "_id",
as: "userDetails"
}
},
{ $unwind: "$userDetails"},
]
).toArray()
.then(result=>{
console.log(result[0])
}).catch(err=>{
res.send(err)
})
}
})
What i want is get all fields from points table and from user table i just want name and username. I have used $project but than its return only fields defined in this.
{ $project: {"userDetails.name":1, "userDetails.username":1,"_id":0} }
Is there any way that $project can be applied separately for user table
You can use pipeline in the lookup if you are using mongodb >= 3.6: https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries
So your code will look like:
app.get('/getAllDetailById',(req,res)=>{
if(db){
// lookup
db.collection("points").aggregate(
[
{ "$addFields": { "enquiry_by": { "$toObjectId": "$enquiry_by" }}},
{
"$lookup" : {
from: "user",
let: { "enquiry_by": "$enquiry_by" },
pipeline: [
{
"$match": {
"$expr": {
"$eq": ["$_id", "$$enquiry_by"]
}
},
"$project": {
"$name": 1,
"$username": 1,
}
},
],
as: "userDetails"
}
},
{ $unwind: "$userDetails"},
]
).toArray()
.then(result=>{
console.log(result[0])
}).catch(err=>{
res.send(err)
})
}
})
I have a output like this but i wanna filter events array by _id's which is same with parent object _id.I wanna filter it according to this condition.
[
{
"_id": "5cc45eb430aaba3cdc045dc0" ,
"word": "Pasta",
"translate": "Makarna",
"kind": "İsim",
"exampleSentence": "asljdalsd",
"__v": 0,
"events": [
{
"_id": "5cc45eb430aaba3cdc045dc0",
"createdAt": "2019-04-27T13:52:15.721Z",
"TenMinutesLater": "2019-04-27T13:52:25.721Z",
"OneWeekLater": "2019-05-04T13:52:15.721Z",
"OneMonthLater": "2019-05-27T13:52:15.721Z",
"FourMonthLater": "2019-08-27T13:52:15.721Z",
"__v": 0
},
{
"_id": "5cc45ee630aaba3cdc045dc1",
"createdAt": "2019-04-27T13:52:15.721Z",
"TenMinutesLater": "2019-04-27T13:52:25.721Z",
"OneWeekLater": "2019-05-04T13:52:15.721Z",
"OneMonthLater": "2019-05-27T13:52:15.721Z",
"FourMonthLater": "2019-08-27T13:52:15.721Z",
"__v": 0
}
]
}
]
I wanna filter my events array which is have same _id property with parent object _id.How should be my query acording to what I want ?
this is my query
Word.find({ _id: req.params.id }, (err, words) => {
if (err) {
console.log(err);
}
const uid = words.map(word => word._id);
console.log(req.params.id);
Word.aggregate([
{
$match: {
_id: {
$in: uid.map(function(id) {
return new mongoose.Types.ObjectId(id);
})
}
}
},
{
$lookup: {
from: "tests",
localField: "eventId",
foreignField: "_id.str",
as: "events"
}
}
])
.then(data => {
res.json(data);
})
.catch(err => {
throw err;
});
});
I want the output like this.How I filter it with parent object's _id ?
[
{
"_id": "5cc45eb430aaba3cdc045dc0" ,
"word": "Pasta",
"translate": "Makarna",
"kind": "İsim",
"exampleSentence": "asljdalsd",
"__v": 0,
"events": [
{
"_id": "5cc45eb430aaba3cdc045dc0",
"createdAt": "2019-04-27T13:52:15.721Z",
"TenMinutesLater": "2019-04-27T13:52:25.721Z",
"OneWeekLater": "2019-05-04T13:52:15.721Z",
"OneMonthLater": "2019-05-27T13:52:15.721Z",
"FourMonthLater": "2019-08-27T13:52:15.721Z",
"__v": 0
]
}
]
Here $lookup will return all the events that matches localfield/foreign field condition as you might already know. If you want to filter results by some other criteria (which is not very clear from your description) you can use pipeline feature in joined collection (introduced in 3.6). A sample pipeline in $lookup (take from official site) would look like this. So as you can see you can execute match on joined collection and filter your events.
db.orders.aggregate([
{
$lookup:
{
from: "warehous`enter code here`es",
let: { order_item: "$item", order_qty: "$ordered" },
pipeline: [
{ $match:
{ $expr:
{ $and:
[
{ $eq: [ "$stock_item", "$$order_item" ] },
{ $gte: [ "$instock", "$$order_qty" ] }
]
}
}
},
{ $project: { stock_item: 0, _id: 0 } }
],
as: "stockdata"
}
}
])
Thanks answer but I finally solved the problem like this.it is working perfectly
Word.find({ _id: req.params.id }, (err, words) => {
if (err) {
console.log(err);
}
const uid = words.map(word => word._id);
Word.aggregate([
{
$match: {
_id: {
$in: uid.map(function(id) {
return mongoose.Types.ObjectId(id);
})
}
}
},
{
$lookup: {
from: "tests",
localField: "_id.str",
foreignField: "eventId",
as: "events"
}
},
{
$addFields: {
events: {
$filter: {
input: "$events",
as: "event",
cond: {
$eq: ["$$event._id", mongoose.Types.ObjectId(req.params.id)]
}
}
}
}
}
])
.then(data => {
res.json(data);
})
.catch(err => {
throw err;
});
});
I have a visits collection where I successfully count the number of visits per location
Visits model:
{
"_id": {
"$oid": "5a3969e2f4ea3e33ac5a523d"
},
"locationId": "5a395ccf210a1d35d0df4a58"
}
locationId above is of type 'Object' as I learned lookup localField and foreignField must be of same type
nodejs code =>
let sort = { "count": -1, "locationId": 1 };
Visit.aggregate([
{
$match:{
$and: [
{ accountId: req.session.passport.user },
{
'details.dateTimeIn': {
$gte: new Date(dateFrom), $lte: new Date(dateTo)
}
}
]
}
},
{
"$group": {
//_id: name,
_id: "$locationId",
count: { $sum: 1 }
}
},
{ $sort: sort }
])
Output is half ok:
[
{
"_id":"5a395ccf210a1d35d0df4a58",
"count":20
}
]
Instead of showing location id id like to show location name. Schema for locations collection is:
{
"_id": {
"$oid": "5a395ccf210a1d35d0df4a58"
"name": "Tower A",
"__v": 0
}
}
Research suggests I need to use $lookup to get that JOIN effect
So I tried
{
"$lookup": {
from: "locations",
localField: "_id",
foreignField: "_id",
as: "locationdetails"
}
}
but the match seems broken. The closest I got was a list of all locations in 'locationdetails'
But with code above here is the empty locationdetails
[
{
"_id":"5a395ddf1d221918d0041313",
"count":20,
"locationdetails":[
]
}
]
What am I missing ?
I have this two schema:
module.exports = mongoose.model('Article', {
title : String,
text : String,
lang : { type: String, default: 'en'},
user : { type : mongoose.Schema.Types.ObjectId, ref: 'User' },
});
var userSchema = mongoose.Schema({
email : String,
name : String,
rating : [{
_id: false,
articleID: {type: mongoose.Schema.Types.ObjectId, ref: 'Article'},
rate: Number
}]
});
module.exports = mongoose.model('User', userSchema);
and i want to calculate the average rating of an user (the average on all rating on its articles).
I tried this:
User.aggregate([
{ $unwind: "$rating" },
{
"$lookup": {
"from": "Article",
"localField": "rating.articleID",
"foreignField": "_id",
"as": "article-origin"
}
}//,
//{ $match: { "article-origin.user" : mongoose.Types.ObjectId(article.user) } }//,
//{ $group : {_id : "$rating.articleID", avgRate : { $avg : "$rating.rate" } } }
]).exec(function (err,result) {
console.log(err);
console.log(JSON.stringify(result));
});
but without success, the lockup always return the field article-origin null.
result:{"_id":"590747e1af02570769c875dc","name":"name","email":"email","rating":{"rate":5,"articleID":"59074a357fe6a307981e7925"},"__v":0,"article-origin":[]}]
Why this is not working ?
Certainly no need for the $lookup operator since the group aggregation operation does not make use of the documents from the articles collection, it only needs a single field i.e. articleID for grouping.
There are two ways you can go about this. If your MongoDB server version is 3.4 or greater, then the $divide, $reduce and $size operators can be applied here to calculate the average without resorting
to flatten the rating array first which can have some performance ramifications if the array is large.
Consider running the following pipeline:
User.aggregate([
{ "$match": { "_id" : mongoose.Types.ObjectId(article.user) } },
{
"$addFields": {
"avgRate": {
"$divide": [
{
"$reduce": {
"input": "$rating",
"initialValue": 0,
"in": { "$sum": ["$$value", "$$this.rate"] }
}
},
{
"$cond": [
{ "$ne": [{ "$size": "$rating" }, 0] },
{ "$size": "$rating" },
1
]
}
]
}
}
}
]).exec(function (err, result) {
console.log(err);
console.log(JSON.stringify(result));
});
If using MongoDB version 3.2 then you would need to $unwind the rating array first:
User.aggregate([
{ "$match": { "_id" : mongoose.Types.ObjectId(article.user) } },
{ "$unwind": "$rating" },
{
"$group": {
"_id": "$_id",
"avgRate": { "$avg": "$rating.rate" }
}
}
]).exec(function (err, result) {
console.log(err);
console.log(JSON.stringify(result));
});
If for some reason you need the $lookup operation, you need to reference the collection name, not the model name, thus the correct aggregate operation should be
User.aggregate([
{ "$unwind": "$rating" },
{
"$lookup": {
"from": "articles", /* collection name here, not model name */
"localField": "rating.articleID",
"foreignField": "_id",
"as": "article-origin"
}
},
{ "$match": { "article-origin.user" : mongoose.Types.ObjectId(article.user) } },
{
"$group": {
"_id": "$_id",
"avgRate": { "$avg": "$rating.rate" }
}
}
]).exec(function (err, result) {
console.log(err);
console.log(JSON.stringify(result));
});
I have Kitchen schema which is structured as below, I want to do a find query on this schema to get a package with particular ID and Date from packages array.
{
"_id" : ObjectId("58aacd498caf670a837e7093"),
"name" : "Kitchen 1",
"packages" : [
{
"package" : ObjectId("58aacd038caf670a837e7091"),
"availibility" : [
{
"date" : ISODate("2015-03-25T00:00:00.000Z"),
"count" : 20
},
{
"date" : ISODate("2016-03-25T00:00:00.000Z"),
"count" : 30
}
]
},
{
"package" : ObjectId("58aacd108caf670a837e7092"),
"availibility" : [
{
"date" : ISODate("2016-03-25T00:00:00.000Z"),
"count" : 10
}
]
}
],
"__v" : 0
}
If I do a find query with package ID(58aacd038caf670a837e7091) and date(2015-03-25T00:00:00.000Z), then response should be like :-
{
"package" : ObjectId("58aacd038caf670a837e7091"),
"date" : ISODate("2015-03-25T00:00:00.000Z")
"count" : 20
}
In mongodb:
You will have to use a loop to see only a single date. For all dates including the matching one for the package id you can do this:
db.collection_name.find({'packages.package':ObjectId("package_id"),'packages.availability.date': ISODate("date")},{'packages.package':1, 'packages.availability':1}).pretty()
In mongoose, I am assuming you have imported the Kitchen schema
Kichen.find({'packages.package':"package_id",'packages.availability.date': "iso_date"}, function(err, package){
if(err)
console.log("There was an error");
if(package == null){
console.log("no package found");
} else {
//do whatever
}
});
You can run an aggregate operation that uses the $filter and $arrayElemAt operators to return the desired fields within a couple of $project pipeline steps.
Consider the following pipeline:
Kitchen.aggregate([
{
"$project": {
"packages": {
"$arrayElemAt": [
{
"$filter": {
"input": "$packages",
"as": "pkg",
"cond": {
"$eq": [
"$$pkg.package",
mongoose.Types.ObjectId("58aacd038caf670a837e7091")
]
}
}
},
0
]
}
}
},
{
"$project": {
"package": "$packages.package",
"availibility": {
"$arrayElemAt": [
{
"$filter": {
"input": "$packages.availibility",
"as": "el",
"cond": {
"$eq": ["$$el.date", new Date("2015-03-25")]
}
}
},
0
]
}
}
},
{
"$project": {
"_id": 0,
"package": 1,
"date": "$availibility.date",
"count": "$availibility.count"
}
}
]).exec(function(err, docs){
if (err) throw err;
console.log(docs);
})
You can compare values in loop:
db.so.find().forEach(function(po){
po.packages.forEach(function(co){
co.availibility.forEach(function(o){
if(co.package=='58aacd038caf670a837e7091'
&&
String(ISODate("2015-03-25T00:00:00.000Z"))==String(o.date)
){
o.package=co.package;
printjson(o);
}
})
})
});
{
"date" : ISODate("2015-03-25T00:00:00Z"),
"count" : 20,
"package" : ObjectId("58aacd038caf670a837e7091")
}