Find query for MongoDb schema design - node.js

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")
}

Related

How can i do elemMatch inside array using mongodb?

I have below user details in my bookings collection
{
"_id" : ObjectId("609a382b589346973c84c6fe"),
"Name" : "abc",
"UserId":1
"Status" : "Pending",
"BookingData" : {
"Date" : ISODate("2021-04-30T04:00:00.000Z"),
"info" : [],
"BookingDataMethod" : "avf",
"Message" : null,
"products" : [
{
"_id" : ObjectId("60a4e92775e5de3570578820"),
"ProductName" : "Test1",
"ProductID" : ObjectId("60a4e92475e5de357057880a"),
"IsDeliveryFailed" : "Yes"
},
{
"_id" : ObjectId("60a4e92775e5de357057881f"),
"ProductName" : "Test2",
"ProductID" : ObjectId("60a4e92475e5de357057880d")
}
],
}
}
I have prepared a query for the below conditions and when I run the below query I should get the "UserId":1 documents but I got 0 records
condition 1: products should not be null
condition 2: ProductID should exist in the products array and should not be null
condition 3: IsDeliveryFailed should not be "Yes"
Based on the above user only one product got delivery failed(IsDeliveryFailed": "Yes") so when I run this query it should return "UserId":1 document. if both products "IsDeliveryFailed": "Yes" then
we should not get this user
Query
db.getCollection('bookings').find({
"$and": [
{ "BookingData.products": { $ne: [] } },
{ "BookingData.products": {"$elemMatch":{ "ProductID": { "$exists": true ,$ne: null } }} },
{ "BookingData.products": {"$elemMatch":{ "IsDeliveryFailed": { $ne: 'Yes' } }} }
]
})
Could someone please tell me the issue on the above query or please help me to prepare a query for the above condition?
I think you can do it with aggregations
db.collection.aggregate([
{
$match: {
"BookingData.products": { "$exists": true }
}
},
{
$set: {
"BookingData.products": {
"$filter": {
"input": "$BookingData.products",
"cond": {
$and: [
{ $ne: [ "$$this.ProductID", undefined ] },
{ $ne: [ "$$this._id", null ] },
{ $ne: [ "$$this.IsDeliveryFailed", "Yes" ] }
]
}
}
}
}
},
{
$match: {
$expr: {
$ne: [ "$BookingData.products", [] ]
}
}
}
])
Working Mongo playground

How to include empty array when filtering an array

Here is my item model.
const itemSchema = new Schema({
name: String,
category: String,
occupied: [Number],
active: { type: Boolean, default: true },
});
I want to filter 'occupied' array. So I use aggregate and unwind 'occupied' field.
So I apply match query. And group by _id.
But if filtered 'occupied' array is empty, the item disappear.
Here is my code
Item.aggregate([
{ $match: {
active: true
}},
{ $unwind:
"$occupied",
},
{ $match: { $and: [
{ occupied: { $gte: 100 }},
{ occupied: { $lt: 200 }}
]}},
{ $group : {
_id: "$_id",
name: { $first: "$name"},
category: { $first: "$category"},
occupied: { $addToSet : "$occupied" }
}}
], (err, items) => {
if (err) throw err;
return res.json({ data: items });
});
Here is example data set
{
"_id" : ObjectId("59c1bced987fa30b7421a3eb"),
"name" : "printer1",
"category" : "printer",
"occupied" : [ 95, 100, 145, 200 ],
"active" : true
},
{
"_id" : ObjectId("59c2dbed992fb91b7421b1ad"),
"name" : "printer2",
"category" : "printer",
"occupied" : [ ],
"active" : true
}
The result above query
[
{
"_id" : ObjectId("59c1bced987fa30b7421a3eb"),
"name" : "printer1",
"category" : "printer",
"occupied" : [ 100, 145 ],
"active" : true
}
]
and the result I want
[
{
"_id" : ObjectId("59c1bced987fa30b7421a3eb"),
"name" : "printer1",
"category" : "printer",
"occupied" : [ 100, 145 ],
"active" : true
},
{
"_id" : ObjectId("59c2dbed992fb91b7421b1ad"),
"name" : "printer2",
"category" : "printer",
"occupied" : [ ],
"active" : true
}
]
how could I do this??
Thanks in advance.
In the simplest form, you keep it simply by not using $unwind in the first place. Your conditions applied imply that you are looking for the "unique set" of matches to specific values.
For this you instead use $filter, and a "set operator" like $setUnion to reduce the input values to a "set" in the first place:
Item.aggregate([
{ "$match": { "active": true } },
{ "$project": {
"name": 1,
"category": 1,
"occupied": {
"$filter": {
"input": { "$setUnion": [ "$occupied", []] },
"as": "o",
"cond": {
"$and": [
{ "$gte": ["$$o", 100 ] },
{ "$lt": ["$$o", 200] }
]
}
}
}
}}
], (err, items) => {
if (err) throw err;
return res.json({ data: items });
});
Both have been around since MongoDB v3, so it's pretty common practice to do things this way.
If for some reason you were still using MongoDB 2.6, then you could apply $map and $setDifference instead:
Item.aggregate([
{ "$match": { "active": true } },
{ "$project": {
"name": 1,
"category": 1,
"occupied": {
"$setDifference": [
{ "$map": {
"input": "$occupied",
"as": "o",
"in": {
"$cond": {
"if": {
"$and": [
{ "$gte": ["$$o", 100 ] },
{ "$lt": ["$$o", 200] }
]
},
"then": "$$o",
"else": false
}
}
}},
[false]
]
}
}}
], (err, items) => {
if (err) throw err;
return res.json({ data: items });
});
It's the same "unique set" result as pulling the array apart, filtering the items and putting it back together with $addToSet. The difference being that its far more efficient, and retains ( or produces ) an empty array without any issues.

how to return response using mongo's aggregate?

I'm starting with mongodb, I'm using aggregate function which gives me the last user of the last element into the sampleStatus array. (I mean the latest record added to sampleStatus)
I have a collection of samples like this :
{
"_id" : ObjectId("58d6cbc14124691cd8154d72"),
"correlativeCode" : "CSLLPA53E20M017W",
"registrationMethod" : "taken",
"originPlace" : "SOMEPLACE",
"temperature" : 16,
"sampleStatus" : [
{
"nameStatus" : "status1",
"place" : "place1",
"rejectionReason" : "Nothing",
"user" : "user1",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status2",
"place" : "place2",
"rejectionReason" : "Nothing",
"user" : "user4",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status3",
"place" : "place3",
"rejectionReason" : "Nothing",
"user" : "user3",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status4",
"place" : "place4",
"rejectionReason" : "Nothing",
"user" : "user1",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status5",
"place" : "place5",
"rejectionReason" : "Nothing",
"user" : "user5",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
}
]
}
This is the function I'm using:
db.collection.aggregate([
{ "$match": { "correlativeCode": "CSLLPA53E20M017W" } },
{ "$redact": {
"$cond": [
{ "$eq": [
{ "$let": {
"vars": {
"item": { "$arrayElemAt": [ "$sampleStatus", -1 ] }
},
"in": "$$item.user"
} },
"user5"
] },
"$$KEEP",
"$$PRUNE"
]
}}
])
When I use this in mongodb's console, it works.. but, when I try to adapt this in a controller.js
VerifySample: function (req, res) {
var id = req.body.idSample;
var idUser=req.body.currentuser;
SamplePatientModel.aggregate([
{ $match: { _id: id } },
{ $redact: {
$cond: [
{ $eq: [
{ $let: {
vars: {
"item": { $arrayElemAt: [ "$sampleStatus", -1 ] }
},
in: "$$item.user"
} },
idUser
] },
"$$KEEP",
"$$PRUNE"
]
}}
],
function(err, _SamplePatient) {
console.log('entry function');
if (err) {
console.log('Entry err');
return res.status(500).json({message: 'Error SamplePatient', error: err});
}
//No results
if(!_SamplePatient){
console.log('no results ');
return res.status(404).json({message: 'error', error: err});
}
console.log('Got it');
console.log(_SamplePatient);
return res.status(200).json(_SamplePatient);
}
);}
It gives me following response:
[]
console.log(_SamplePatient) doesn't show anything
the words "entry function" are printed in console
what am I doing wrong?
Please, help me.
Thanks.
Casting ObjectId in mongoose is not supported in aggregation pipeline.
So you've to explicitly cast the string value to ObjectId in the aggregation pipeline.
Update your match stage to below.
{ $match: { _id: mongoose.Types.ObjectId(req.body.idSample) } }
Here is the issue
https://github.com/Automattic/mongoose/issues/1399
Mongoose Docs:
http://mongoosejs.com/docs/api.html#model_Model.aggregate

Date operation insede and array (aggreagation)

I want to build online test application using mongoDB and nodeJS. And there is a feature which admin can view user test history (with date filter option).
How to do the query, if I want to display only user which the test results array contains date specified by admin.
The date filter will be based on day, month, year from scheduledAt.startTime, and I think I must use aggregate framework to achieve this.
Let's say I have users document like below:
{
"_id" : ObjectId("582a7b315c57b9164cac3295"),
"username" : "lalalala#gmail.com",
"displayName" : "lalala",
"testResults" : [
{
"applyAs" : [
"finance"
],
"scheduledAt" : {
"endTime" : ISODate("2016-11-15T16:00:00.000Z"),
"startTime" : ISODate("2016-11-15T01:00:00.000Z")
},
"results" : [
ObjectId("582a7b3e5c57b9164cac3299"),
ObjectId("582a7cc25c57b9164cac329d")
],
"_id" : ObjectId("582a7b3e5c57b9164cac3296")
},
{
.....
}
],
"password" : "andi",
}
testResults document:
{
"_id" : ObjectId("582a7cc25c57b9164cac329d"),
"testCategory" : "english",
"testVersion" : "EAX",
"testTakenTime" : ISODate("2016-11-15T03:10:58.623Z"),
"score" : 2,
"userAnswer" : [
{
"answer" : 1,
"problemId" : ObjectId("581ff74002bb1218f87f3fab")
},
{
"answer" : 0,
"problemId" : ObjectId("581ff78202bb1218f87f3fac")
},
{
"answer" : 0,
"problemId" : ObjectId("581ff7ca02bb1218f87f3fad")
}
],
"__v" : 0
}
What I have tried until now is like below. If I want to count total document, which part of my aggregation framework should I change. Because in query below, totalData is being summed per group not per whole returned document.
User
.aggregate([
{
$unwind: '$testResults'
},
{
$project: {
'_id': 1,
'displayName': 1,
'testResults': 1,
'dayOfTest': { $dayOfMonth: '$testResults.scheduledAt.startTime' },
'monthOfTest': { $month: '$testResults.scheduledAt.startTime' },
'yearOfTest': { $year: '$testResults.scheduledAt.startTime' }
}
},
{
$match: {
dayOfTest: date.getDate(),
monthOfTest: date.getMonth() + 1,
yearOfTest: date.getFullYear()
}
},
{
$group: {
_id: {id: '$_id', displayName: '$displayName'},
testResults: {
$push: '$testResults'
},
totalData: {
$sum: 1
}
}
},
])
.then(function(result) {
res.send(result);
})
.catch(function(err) {
console.error(err);
next(err);
});
You can try something like this. Added the project stage to keep the test results if any of result element matches on the date passed. Add this as the first step in the pipeline and you can add the grouping stage the way you want.
$map applies an equals comparison between the date passed and start date in each test result element and generates an array with true and false values. $anyElementTrue inspects this array and returns true only if there is atleast one true value in the array. Match stage to include only elements with matched value of true.
aggregate([{
"$project": {
"_id": 1,
"displayName":1,
"testResults": 1,
"matched": {
"$anyElementTrue": {
"$map": {
"input": "$testResults",
"as": "result",
"in": {
"$eq": [{ $dateToString: { format: "%Y-%m-%d", date: '$$result.scheduledAt.startTime' } }, '2016-11-15']
}
}
}
}
}
}, {
"$match": {
"matched": true
}
}])
Alternative Version:
Similar to the above version but this one combines both the project and match stage into one. The $cond with $redact accounts for match and when match is found it keeps the complete tree or else discards it.
aggregate([{
"$redact": {
"$cond": [{
"$anyElementTrue": {
"$map": {
"input": "$testResults",
"as": "result",
"in": {
"$eq": [{
$dateToString: {
format: "%Y-%m-%d",
date: '$$result.scheduledAt.startTime'
}
}, '2016-11-15']
}
}
}
},
"$$KEEP",
"$$PRUNE"
]
}
}])

Create Price Range in mongo with aggregation pipeline with Nodejs

Wan't create Price range Using mongodb aggregation pipeline..
while using elastic search or solr we can directly get price filter range value... How can i create price range according to my products price, if there is no product in that range then don't create that range...
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 1200
},
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 200
},
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 2000
},
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 2020
},
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 100
},
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 3500
},
{
"_id" : ObjectId("5657412ddb70397479575d1d"),"price" : 3900
}
From above i have to create price range as par product price like filter available in flipkart OR myntra using Mongo aggregation...
[
{
range : '100-200',
count : 2
},
{
range : '1200-2020',
count : 3
},
{
range : '3500-3900',
count : 2
}
]
Within the aggregation framework pipeline, you can take advantage of the $cond operator in the $project stage to create an extra field that denotes the range the price falls in, and then use the $group step to get the counts:
var pipeline = [
{
"$project": {
"price": 1,
"range": {
"$cond": [
{
"$and": [
{ "$gte": ["$price", 100] },
{ "$lte": ["$price", 200] }
]
},
"100-200",
{
"$cond": [
{
"$and": [
{ "$gte": ["$price", 1200] },
{ "$lte": ["$price", 2020] }
]
},
"1200-2020", "2021-above"
]
}
]
}
}
},
{
"$group": {
"_id": "$range",
"count": { "$sum": 1 }
}
},
{
"$project": {
"_id": 0,
"range": "$_id",
"count": 1
}
}
];
collection.aggregate(pipeline, function (err, result){
if (err) {/* Handle err */};
console.log(JSON.stringify(result, null, 4));
});

Resources